Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Bit Conversion

 
MI Mohammed
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone help me out on short cuts
of converting decimal and octal numbers
to and from binary.
My exams is next saturday.
 
Thomas Fischer
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hope that helps.
BigInteger is not a objective of the SCJP as far as i know.



Output is:
1001
1001
9

Good luck for your exams.
 
Saurabh Khanna
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you can also use some ready-made methods in Integer class:




And, there is also a toHexString(..) and toOctalString(..)

Good luck with your exam! and let us know how it goes...

Saurabh
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Link
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mi, a number of "shortcuts" were offered under your previous post:

http://www.coderanch.com/t/248524/java-programmer-SCJP/certification/Bits-Conversion

If these didn't help, then what exactly are you looking for?

(BigInteger and BigDecimal are not on the SCJP 1.4 exam.)
 
MI Mohammed
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marc,
Result from my last post on this topic
was very helpful, i now know that in hex
0000 --> 0
so to get other simply add 1 to 0000
downwards.
i just need a similar formula
for oct and binary.
cheers
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you understand the rationale behind these representations?

For example, decimal numbers use ten as a base. This means that each digit represents a quantity multiplied by ten raised to some power. Specifically, the right digit is multiplied by 10^0, the digit to the left of that is multiplied by 10^1, the digit to the left of that is multiplied by 10^2, and so on.

So in base ten, 987 means (9 * 10^2) + (8 * 10^1) + (7 * 10^0).

In binary, the base is two, so 101 means (1 * 2^2) + (0 * 2^1) + (1 * 2^0).

In octal, the base is eight, so 765 means (7 * 8^2) + (6 * 8^1) + (5 * 8^0).

In hexadecimal, the base is 16 (with letters a-f used to represent 10-15), so c9a means (12 * 16^2) + (9 * 16^1) + (10 * 16^0).

With this understanding, do you see the patterns below?
 
MI Mohammed
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike,
Thanks a million. Got to read your reply after
my exams. Well i passed with 77%, not quite happy with it
cause i made some silly errors.
Once thanks

SCJP 1.4
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic