Connecting Tech Pros Worldwide Help | Site Map

signed to unsigned. How does this work?

  #1  
Old May 23rd, 2007, 02:25 PM
Robert Smith
Guest
 
Posts: n/a
Why/How does this work?

I know that if I want to convert a signed byte (ie 0x7F) to unsigned
number I can promote it to an integer, like this:

byte a = (byte)0xAB; // -85
int b = a & 0xFF; // 171

The bit pattern is 10101011 (0xAB)
If I AND it with 11111111 (0xFF)
The result is 10101011

ie. the same number! so why does this change the number to an unsigned
(positive) number?


  #2  
Old May 23rd, 2007, 03:35 PM
Lew
Guest
 
Posts: n/a

re: signed to unsigned. How does this work?


Robert Smith wrote:
Quote:
Why/How does this work?
>
I know that if I want to convert a signed byte (ie 0x7F) to unsigned
number I can promote it to an integer, like this:
>
byte a = (byte)0xAB; // -85
int b = a & 0xFF; // 171
>
The bit pattern is 10101011 (0xAB)
If I AND it with 11111111 (0xFF)
The result is 10101011
>
ie. the same number!
It is not the same number. You left out all the high bits where the
difference is apparent.

a is a byte, b is an int.

a promotes to int in order to participate in the mask operation; the promoted
value is
0xFFFFFFAB.

Because you masked out the high bits of a's promoted value on purpose, b is
0x000000AB.

Quite different.
Quote:
so why does this change the number to an unsigned (positive) number?
Because you masked out all the high bits, including the sign bit, on purpose.

0xFF is an int, and a positive one at that.

0xFF == 0x000000FF

The result of the & is 0x000000AB.

--
Lew
  #3  
Old May 26th, 2007, 03:05 AM
Robert Smith
Guest
 
Posts: n/a

re: signed to unsigned. How does this work?


Thanks so much for explaining that

"Lew" <lew@nospam.lewscanon.comwrote in message
news:LcGdnRlLK8aWzMnbnZ2dnUVZ_vWtnZ2d@comcast.com. ..
Quote:
Robert Smith wrote:
Quote:
Why/How does this work?

I know that if I want to convert a signed byte (ie 0x7F) to unsigned
number I can promote it to an integer, like this:

byte a = (byte)0xAB; // -85
int b = a & 0xFF; // 171

The bit pattern is 10101011 (0xAB)
If I AND it with 11111111 (0xFF)
The result is 10101011

ie. the same number!
>
It is not the same number. You left out all the high bits where the
difference is apparent.
>
a is a byte, b is an int.
>
a promotes to int in order to participate in the mask operation; the
promoted
Quote:
value is
0xFFFFFFAB.
>
Because you masked out the high bits of a's promoted value on purpose, b
is
Quote:
0x000000AB.
>
Quite different.
>
Quote:
so why does this change the number to an unsigned (positive) number?
>
Because you masked out all the high bits, including the sign bit, on
purpose.
Quote:
>
0xFF is an int, and a positive one at that.
>
0xFF == 0x000000FF
>
The result of the & is 0x000000AB.
>
--
Lew

  #4  
Old May 26th, 2007, 03:35 PM
Lew
Guest
 
Posts: n/a

re: signed to unsigned. How does this work?


Robert Smith wrote:
Quote:
Thanks so much for explaining that
Your avoidance of top-posting in the future will be thanks aplenty.

--
Lew
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
casting to unsigned char for is*() and to*() functions mr_semantics@hotmail.com answers 14 November 15th, 2005 12:21 AM
clc selected threads (30-jan-2005 to 31-jan-2005) #1 sathyashrayan answers 6 November 14th, 2005 06:51 PM
signed char and unsigned char difference dam_fool_2003@yahoo.com answers 9 November 14th, 2005 11:04 AM
Convert a binary value to unsigned decimal value Golan answers 7 November 13th, 2005 11:58 PM