Arjun Reddy

Ranch Hand

Posts: 629

posted 9 years ago

Java integers uses Twos Complement format. First, the sign bit is set, so the number is negative. For negative numbers, to figure out the number you need to negate the number to a positive number. And then calculate that positive number, as usual.

X = 1111 1000 0000 0000 0000 0000 0000 0000

~X = 0000 0111 1111 1111 1111 1111 1111 1111

~X + 1 = -X = 0000 1000 0000 0000 0000 0000 0000 0000

-X = 134217728

X = -134217728

For more information, try google "twos complement".

Henry

How can I convert 1111 1000 0000 0000 0000 0000 0000 0000 to decimal

Java integers uses Twos Complement format. First, the sign bit is set, so the number is negative. For negative numbers, to figure out the number you need to negate the number to a positive number. And then calculate that positive number, as usual.

X = 1111 1000 0000 0000 0000 0000 0000 0000

~X = 0000 0111 1111 1111 1111 1111 1111 1111

~X + 1 = -X = 0000 1000 0000 0000 0000 0000 0000 0000

-X = 134217728

X = -134217728

For more information, try google "twos complement".

Henry

posted 9 years ago

First, convert the hexidecimal to binary, and then convert the binary to decimal.

DEADCAFE = 1101 1110 1010 1101 1100 1010 1111 1110

To convert to decimal, as with the previous post, you need to use Twos Complement.

[Note: You can go directly from Hexidecimal to Decimal, but you should confirm that it is a positive number first]

Henry

[ September 17, 2008: Message edited by: Henry Wong ]

How can I convert 0xDeadCafe to decimal

First, convert the hexidecimal to binary, and then convert the binary to decimal.

DEADCAFE = 1101 1110 1010 1101 1100 1010 1111 1110

To convert to decimal, as with the previous post, you need to use Twos Complement.

[Note: You can go directly from Hexidecimal to Decimal, but you should confirm that it is a positive number first]

Henry

[ September 17, 2008: Message edited by: Henry Wong ]

posted 9 years ago

In hex (base 16), the position furthest to the right represents 16^0 (meaning 16 raised to the 0 power). The position left of that represents 16^1. The next position left of that represents 16^2. And so on.

Also, the characters a, b, c, d, e, and f are used to represent decimal (base 10) values 10, 11, 12, 13, 14, and 15 respectively.

So for example, 0xb32 means:

(2 * 16^0) +

(3 * 16^1) +

(b * 16^2)

...which is...

(2 * 1) +

(3 * 16) +

(11 * 256)

...which is 2 + 48 + 2816 = 2866 in decimal.

Also, the characters a, b, c, d, e, and f are used to represent decimal (base 10) values 10, 11, 12, 13, 14, and 15 respectively.

So for example, 0xb32 means:

(2 * 16^0) +

(3 * 16^1) +

(b * 16^2)

...which is...

(2 * 1) +

(3 * 16) +

(11 * 256)

...which is 2 + 48 + 2816 = 2866 in decimal.

"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

Arjun Reddy

Ranch Hand

Posts: 629

posted 9 years ago

You might want to review your base mathematics. Hexidecimal numbers are just base 16 numbers. But to answer your question, d = 13, whether it is lower or upper case.

Henry

Originally posted by Arjun Reddy:

Thanks Henry.. I will learn twos compliment now Um... Marc, thanks for replying too...does lower case and upper case letters matter? Can I consider samelower case das 13 andUpper case Das 13 too?

You might want to review your base mathematics. Hexidecimal numbers are just base 16 numbers. But to answer your question, d = 13, whether it is lower or upper case.

Henry

Arjun Reddy

Ranch Hand

Posts: 629

posted 9 years ago

That is because the number is negative -- the sign bit is one. You need to take into account twos complement when negative numbers are involved.

Henry

Originally posted by Arjun Reddy:

The reason I asked was because I was confused, the answer for 0xDeadCafe is -559035650.This is not the result I get according to what Marc said. Doing just 13*(16^7) itself is giving a number bigger than the answer.

Thanks.

That is because the number is negative -- the sign bit is one. You need to take into account twos complement when negative numbers are involved.

Henry

Arjun Reddy

Ranch Hand

Posts: 629

Campbell Ritchie

Marshal

Posts: 56521

172

Arjun Reddy

Ranch Hand

Posts: 629

posted 9 years ago

Henry, Then for 0xbeef, the sign bit is also 1(b-->

Thanks.

[ September 20, 2008: Message edited by: Arjun Reddy ]

Originally posted by Henry Wong:

That is because the number is negative -- the sign bit is one. You need to take into account twos complement when negative numbers are involved.

Henry

Henry, Then for 0xbeef, the sign bit is also 1(b-->

**1**011) right, so we have to do a twos complement right? but the answer to this is 48879(twos complement not done here)?

Thanks.

[ September 20, 2008: Message edited by: Arjun Reddy ]

Be Humble... Be Nice.

posted 9 years ago

Is 0xbeef a short? Or is it an int?

If it is a short, then the sign bit is one... ie. the number is negative. And the twos complement should yield an answer of -16657.

If it is an int, then the sign but is zero... because the full number is 0x0000beef. And the number is 48879.

Remember, the sign bit is the last bit, which is dependent on the size of the type.

Henry

[ September 20, 2008: Message edited by: Henry Wong ]

Henry, Then for 0xbeef, the sign bit is also 1(b-->1011) right, so we have to do a twos complement right? but the answer to this is 48879(twos complement not done here)?

Is 0xbeef a short? Or is it an int?

If it is a short, then the sign bit is one... ie. the number is negative. And the twos complement should yield an answer of -16657.

If it is an int, then the sign but is zero... because the full number is 0x0000beef. And the number is 48879.

Remember, the sign bit is the last bit, which is dependent on the size of the type.

Henry

[ September 20, 2008: Message edited by: Henry Wong ]

Arjun Reddy

Ranch Hand

Posts: 629

posted 9 years ago

And I agree with Campbell. Work with 8 bits -- as it is much easier to do the twos complement math on. (which should make it easier to learn twos complement)

But as you have seen, if you are going to confirm the results, makes sure that you cast the type before you print it. As 0xFE may be a byte, but until you cast it, it is considered an int literal.

Henry

Originally posted by Campbell Ritchie:

Try it with 0xFE first; it is a lot easier to handle 8 bits than 32!

And I agree with Campbell. Work with 8 bits -- as it is much easier to do the twos complement math on. (which should make it easier to learn twos complement)

But as you have seen, if you are going to confirm the results, makes sure that you cast the type before you print it. As 0xFE may be a byte, but until you cast it, it is considered an int literal.

Henry