This is a discussion on Bit Operation not working within the Linux Networking forums, part of the Linux Forums category; Hi, Somehow my bit operation always negates the number. I have: int size = 141; char * packet = (char *) malloc ((sizeof(char)); ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi,
Somehow my bit operation always negates the number. I have: int size = 141; char * packet = (char *) malloc ((sizeof(char)); packet[0] = 141 & 0xff; Instead of packet being 8d, ie 141, it becomes ffffff8d. Please help. Thank you, Marcia |
|
|||
|
How are you determining your value?
-- Frank "Marcia Hon" <honm@rogers.com> wrote in message news:itTXb.60518$Ywc1.13857@news04.bloor.is.net.ca ble.rogers.com... > Hi, > Somehow my bit operation always negates the number. > > I have: > int size = 141; > char * packet = (char *) malloc ((sizeof(char)); > > packet[0] = 141 & 0xff; > > Instead of packet being 8d, ie 141, it becomes ffffff8d. > Please help. > > Thank you, > Marcia > > > |
|
|||
|
Marcia Hon wrote:
> Hi, > Somehow my bit operation always negates the number. > > I have: > int size = 141; > char * packet = (char *) malloc ((sizeof(char)); > > packet[0] = 141 & 0xff; > > Instead of packet being 8d, ie 141, it becomes ffffff8d. > Please help. Your first problem is that your parentheses are mismatched around "sizeof". Your second problem is that you're not just printing an individual char, you're converting it to some larger type first, probably int. As part of the conversion, the value in your (signed) char is being sign-extended. Try using unsigned char's for raw data. -Jeff |