Unix Programming - Bit Operation not working

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > February 2004 > Bit Operation not working





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Bit Operation not working
Marcia Hon

2004-02-16, 4:33 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



Frank Hickman

2004-02-16, 5:33 pm

How are you determining your value?

--
Frank

"Marcia Hon" <honm@rogers.com> wrote in message
news:itTXb.60518$Ywc1.13857@news04.bloor.is.net.cable.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
>
>
>



Jeff Schwab

2004-02-17, 1:34 am

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

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com