• Post Reply Bookmark Topic Watch Topic
  • New Topic
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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Converting to Hexadecimal literals

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know that it is easy to convert a small number like 1 to hex which is 0x0001.
But how do you convert a big number like 2147483647 to hex? Is there a formula that I can follow? And I am wondering if in the exam they will test you on converting big numbers to hex.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by May Yoong Cheah:
I know that it is easy to convert a small number like 1 to hex which is 0x0001.
But how do you convert a big number like 2147483647 to hex? Is there a formula that I can follow? And I am wondering if in the exam they will test you on converting big numbers to hex.


You can do a search through this forum and find lots of threads that deal with this.
I find that the best method is to continutally divide the number by the base you want to convert to. Then, the remainders make up the value in the new base. Like this:

Then, your answer is the remainders, in reverse order:
2147483647 = 0x7FFFFFFF
You can do the same to convert to binary - just divide by 2 instead of 16.
Of course, I can't imagine them giving you a number this nasty to do on the exam - the math would take too long.
I hope that helps,
Corey
 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sure there is a better way, but what I usually do is convert to binary first, and then you group every four binary digits together and convert them to HEX.
For example:
2531 is 100111100011 in binary
so the first four digits from the left are 0011 which is a 3 in dec and hex
the next four are 1110 which is a 14 or an E in hex
the next four are 1001 which is a 9 in dec and hex
so 2531 in HEX is 0x3E9
If you don't have enough digits to group by fours, you just add some leading 0s until you do.
It can be a time consuming process and there might be a better way, but that is how I convert to HEX.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
May -
It is good to know how to make these conversions, and the better news is you won't have to on the exam!
On the other hand, the exam will test your knowledge of bit shifting, so you might have to deal with an expression like x << 31.
So it IS important to know how many bits each of the primitive types has, and it IS important to understand 2's complement.
 
May-Yoong Cheah
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Corey, Damien and Bert for all your solutions...
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bert,
To what extent will we need to know how to evaluate bit shifting expressions? Is there any shortcut(quick way) for this?
thanks.
Mansi
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you use the >> operator, then you divide the left operator by 2 to the second operator.
Ex.
12 >> 2
...is really
12 / 2^2
...which is really
12 / 4
...or 3
If you use the << operator, then you multiply the left operator by 2 times the second operator.
Ex.
12 << 2
...is really
12 * 2^2
...which is really
12 * 4
...or 48
Hope that helps!
[ April 07, 2003: Message edited by: Tom Purl ]
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mansi -
You are very likely to encounter the following topics grouped together into a single question.
- The functionality of >>, or <<, or >>>
- The size of integral primitives in bits,
- How two's complement works.
For example:
short x = 1 << 15;
Is x a really small number?
Is x a really big positive number?
Is x a really big negative number?
See how all three of those topics are used together. But notice that you don't have to be a human calculator.
-Bert
reply
    Bookmark Topic Watch Topic
  • New Topic