Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Bit Conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Bit Conversion" Watch "Bit Conversion" New topic
Author

Bit Conversion

MI Mohammed
Ranch Hand

Joined: Feb 16, 2005
Posts: 146
Can someone help me out on short cuts
of converting decimal and octal numbers
to and from binary.
My exams is next saturday.


SCJA(Beta) SCJP 1.4 SCWCD 1.4 SCBCD 1.3 SCBCD 5.0 beta <br />The more practice we get, the better we are at the exams and in life in general. Pls join me at My DEN.
Thomas Fischer
Greenhorn

Joined: May 26, 2004
Posts: 6
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

Joined: Apr 03, 2005
Posts: 30
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

Joined: Dec 20, 2001
Posts: 3271
Link


SCJP Tipline, etc.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.)


"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
MI Mohammed
Ranch Hand

Joined: Feb 16, 2005
Posts: 146
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Feb 16, 2005
Posts: 146
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Bit Conversion