Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
July 3rd, 2013, 09:36 AM
 deamonata
Registered User

Join Date: Jul 2013
Posts: 6
Time spent in forums: 1 h 4 m 58 sec
Reputation Power: 0
Quick question

Hi fully aware this is a simple question but could someone clarify what this line means.

Code:
`(((int16_t)buffer[0]) << 8) | buffer[1]`

Thanks

#2
July 3rd, 2013, 10:16 AM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,842
Time spent in forums: 3 Months 23 h 25 m 9 sec
Reputation Power: 2199
Bit-wise operation called a left shift.

Apparently, buffer has been declared as an array of int8_t, which would be 8-bit integer. Apparently, you're wanting to use the first two elements in that array to create a 16-bit integer value in which buffer[0] is the most significant byte (MSB). So you cast buffer[0] as an int16_t, shift it left by 8 bits, and OR in buffer[1].

If that doesn't make sense, then review bit-wise operators.

#3
July 4th, 2013, 05:56 AM
 deamonata
Registered User

Join Date: Jul 2013
Posts: 6
Time spent in forums: 1 h 4 m 58 sec
Reputation Power: 0
thanks I have covered bitwise operators before, but it was a few years ago and I was having a bit of a brain fart :P. Quick question regarding pointers if i have the variables x,y,and z in my main code and I want to change it inside of a function, I should use pointers right? Would this be the correct way to do it?

Code:
```void main(void)
{
int x;
int y;
int z;

function(&x,&y,&z)

}```

then should my function be:
Code:
```void function(int* x,int* y,int* z)
{
/*
Variable manipulation here e.g
Z=x+y;
*/
return
}```

#4
July 4th, 2013, 07:21 AM
 chaitanya.kk
Registered User

Join Date: Jul 2013
Posts: 6
Time spent in forums: 1 h 17 m 21 sec
Reputation Power: 0
pointers usage to change their values

-> x,y,z are integers in main function,

-> function(&x,&y,&z); is function call we usually say call by reference

-> and the formal arguments are also x,y,z these are integer pointers, not integers, the variable in main and pointer variable in function are different.

-> and to change the values of x,y,z in main u must change the value contained by the pointer variables x,y,z in function definition.

-> *z= *x+*y; will add contents of x & y and stores in z.

#5
July 4th, 2013, 07:54 AM
 Homi@th
Contributing User

Join Date: Jun 2013
Posts: 40
Time spent in forums: 1 Day 8 h 58 m 5 sec
Reputation Power: 18

Example http://www.cs.utah.edu/~germain/PPS/Topics/C_Language/c_functions.html

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Quick question