programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Tim Cooke
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Liutauras Vilda
Sheriffs:
• Rob Spoor
• Junilu Lacar
• paul wheaton
Saloon Keepers:
• Stephan van Hulst
• Tim Moores
• Tim Holloway
• Carey Brown
• Scott Selikoff
Bartenders:
• Piet Souris
• Jj Roberts
• fred rosenberger

Ranch Hand
Posts: 629
• Number of slices to send:
Optional 'thank-you' note:
How can I convert 0xDeadCafe to decimal and 1111 1000 0000 0000 0000 0000 0000 0000 to decimal. Sorry am very weak at Mathematics

Thanks.

author
Posts: 23923
142
• Number of slices to send:
Optional 'thank-you' note:

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

Henry

Henry Wong
author
Posts: 23923
142
• Number of slices to send:
Optional 'thank-you' note:

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 ]

Sheriff
Posts: 11343
• Number of slices to send:
Optional 'thank-you' note:
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.

Arjun Reddy
Ranch Hand
Posts: 629
• Number of slices to send:
Optional 'thank-you' note:
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 same lower case d as 13 and Upper case D as 13 too?
[ September 17, 2008: Message edited by: Arjun Reddy ]

Henry Wong
author
Posts: 23923
142
• Number of slices to send:
Optional 'thank-you' note:

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 same lower case d as 13 and Upper case D as 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
• Number of slices to send:
Optional 'thank-you' note:
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.

Henry Wong
author
Posts: 23923
142
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
Okie Henry and Marc Thanks a lot for answering.

Marshal
Posts: 75836
361
• Number of slices to send:
Optional 'thank-you' note:
Try it with 0xFE first; it is a lot easier to handle 8 bits than 32!

Arjun Reddy
Ranch Hand
Posts: 629
• Number of slices to send:
Optional 'thank-you' note:

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-->1011) 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 ]

Henry Wong
author
Posts: 23923
142
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
Okie.. I got it. Thanks Henry.

Henry Wong
author
Posts: 23923
142
• Number of slices to send:
Optional 'thank-you' note:

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

 Consider Paul's rocket mass heater.