# bit values for bytes

sushil bhogale

Greenhorn

Posts: 8

posted 8 years ago

First of all, "-42" is not represented as "1010 1010". "1010 1010" is the representation of "-86". The representation of "-42" is "1101 0110".

To answer your question, to upcast a byte to an int, just sign extend the byte -- meaning the last bit is extended to the other bits. Hence, "1101 0110" becomes "1111 1111 1111 1111 1111 1111 1101 0110".

Henry

- 0

Originally posted by sushil bhogale:

byte = -42; It gets represented in bits as 1010 1010. How it gets represented as int i.e. in 32 bits ?

First of all, "-42" is not represented as "1010 1010". "1010 1010" is the representation of "-86". The representation of "-42" is "1101 0110".

To answer your question, to upcast a byte to an int, just sign extend the byte -- meaning the last bit is extended to the other bits. Hence, "1101 0110" becomes "1111 1111 1111 1111 1111 1111 1101 0110".

Henry

posted 8 years ago

- 0

How to represent -42 as bits:

It's a 42 in binary is 0010 1010. Inverting these bits gives 1101 0101. Then adding one gives 1101 0110. How to figure out what the byte 1010 1010 represents:

The leading bit is 1, indicating a Inverting these bits gives 0101 0101. And adding one gives 0101 0110. This is 86, so the original byte represents -86.

*negative*value, so the process is...

*negative*value, so the process is...

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." *~Joe Strummer*

sscce.org

I agree. Here's the link: http://aspose.com/file-tools |