Bit Operation not working

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)); ...


Go Back   Usenet Forums > Linux Forums > Linux Networking

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-15-2004
Marcia Hon
 
Posts: n/a
Default Bit Operation not working

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



Reply With Quote
  #2 (permalink)  
Old 02-17-2004
Frank Hickman
 
Posts: n/a
Default Re: Bit Operation not working

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
>
>
>



Reply With Quote
  #3 (permalink)  
Old 02-17-2004
Jeff Schwab
 
Posts: n/a
Default Re: Bit Operation not working

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

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 06:15 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0