473,326 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

signed to unsigned. How does this work?

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?
May 23 '07 #1
3 15701
Lew
Robert Smith wrote:
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.
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
May 23 '07 #2
Thanks so much for explaining that

"Lew" <le*@nospam.lewscanon.comwrote in message
news:Lc******************************@comcast.com. ..
Robert Smith wrote:
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.
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

May 26 '07 #3
Lew
Robert Smith wrote:
Thanks so much for explaining that
Your avoidance of top-posting in the future will be thanks aplenty.

--
Lew
May 26 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: MiniDisc_2k2 | last post by:
Okay, here's a question about the standard. What does it say about unsigned/signed mismatches in a comparison statement: char a = 3; unsigned char b = 255; if (a<b) Now what's the real...
8
by: Rade | last post by:
Following a discussion on another thread here... I have tried to understand what is actually standardized in C++ regarding the representing of integers (signed and unsigned) and their conversions....
6
by: Sona | last post by:
Hi, What is the difference between a signed 0x00 (NULL, or 0) and an unsigned 0x00? Can there be one? If I do the following: char var; var = 0x00; what should var hold? and if it was an...
9
by: Fred Ma | last post by:
Hello, I've been trying to clear up a confusion about integer promotions during expression evaluation. I've checked the C FAQ and C++ FAQ (they are different languages, but I was hoping one...
9
by: dam_fool_2003 | last post by:
For int data type the default range starts from signed to unsigned. If we don't want negative value we can force an unsigned value. The same goes for long also. But I don't understand why we have...
10
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the...
10
by: =?iso-8859-2?B?SmFuIFJpbmdvuQ==?= | last post by:
Hello everybody, this is my first post to a newsgroup at all. I would like to get some feedback on one proposal I am thinking about: --- begin of proposal --- Proposal to add...
26
by: John Harrison | last post by:
I have a problem. I want to compare an integral value, n, against three half open ranges as follows [-A, 0) // range 1 [0, B) // range 2 [B, C} // range 3 Each range corresponds to a...
7
by: somenath | last post by:
Hi All, I am trying to undestand "Type Conversions" from K&R book.I am not able to understand the bellow mentioned text "Conversion rules are more complicated when unsigned operands are...
28
by: Fore | last post by:
Hello I am looking for some effecient way to convert a 32 bit unsigned integer to a 16 bit signed integer. All I want is the lower 16 bits of the 32 bit unsigned integer , with bit 15 (0..15) to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.