Networking Forums

Networking Forums > Computer Networking > Linux Networking > Bit Operation not working

Reply
Thread Tools Display Modes

Bit Operation not working

 
 
Marcia Hon
Guest
Posts: n/a

 
      02-15-2004, 10:42 PM
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
 
 
 
 
Frank Hickman
Guest
Posts: n/a

 
      02-17-2004, 05:19 AM
How are you determining your value?

--
Frank

"Marcia Hon" <(E-Mail Removed)> wrote in message
news:itTXb.60518$(E-Mail Removed) 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
 
Jeff Schwab
Guest
Posts: n/a

 
      02-17-2004, 01:18 PM
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
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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Operation of DNS server Lawrence D'Oliveiro Linux Networking 8 11-06-2010 06:24 AM
802.11n receiver operation myhanguk Wireless Internet 0 10-19-2007 08:44 AM
11b/11g Operation Andy Home Networking 4 04-06-2004 06:23 PM
{squid]operation not permitted Vincent GAUVIN Linux Networking 1 12-26-2003 07:45 AM
Socket Operation Paul Windows Networking 6 09-07-2003 09:54 AM



1 2 3 4 5 6 7 8 9 10 11