Help coderanch get a
new server
by contributing to the fundraiser
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

# Binary Representation of integers

Greenhorn
Posts: 1
• Number of slices to send:
Optional 'thank-you' note:
I'm recently studying for the java 2 cert from sun and I keep coming across a question like this:

The question is, what is the printed result. My question is this, is there a quick way to convert numbers to their binary representations such as 12 = 1100 and 13 = 1101. This is really the only thing I haven't been able to find an answer to. I'd like to know a quick way so I don't spend 1/2 hour on one question trying to calculate a binary representation of a number.

Thanks!
Kevin

[HENRY: Added code tags]
[ October 24, 2006: Message edited by: Henry Wong ]

Java Cowboy
Posts: 16084
88
• Number of slices to send:
Optional 'thank-you' note:
Well, what's your current method that takes you so much time to convert a number to its binary form? For small numbers like 3, 6, 12, 13 it isn't very difficult.

Just look at what values the different bits represent. For example, if I have an 8-bit number, the values of the bits from left to right are:

2^7 / 2^6 / 2^5 / 2^4 / 2^3 / 2^2 / 2^1 / 2^0 = 128 / 64 / 32 / 16 / 8 / 4 / 2 / 1

To convert a number to binary, you just write it as a sum of the numbers above, and that gives you your "1" bits. For example:

3 = 2 + 1 = 00000011
6 = 4 + 2 = 00000110
12 = 8 + 4 = 00001100
13 = 8 + 4 + 1 = 00001101
79 = 64 + 8 + 4 + 2 + 1 = 01001111

Sheriff
Posts: 11343
• Number of slices to send:
Optional 'thank-you' note:
Welcome to JavaRanch!

I draw a simple table, then fill in 0's and 1's from left to right. For example, adding 83 and 10 in binary...

Since this is an SCJP question, I'm moving this to our SCJP forum. Please continue this discussion there.
[ October 24, 2006: Message edited by: marc weber ]

Ranch Hand
Posts: 31
• Number of slices to send:
Optional 'thank-you' note:
...or you could use the method:

Returns a string representation of the integer argument as an unsigned integer in base 2.

marc weber
Sheriff
Posts: 11343
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by samir Sadiki:
...or you could use the method...

Well, when you're actually sitting for the exam, limited to whatever writing tools the testing center provides...
[ October 24, 2006: Message edited by: marc weber ]

 We don't have time to be charming! Quick, read this tiny ad: We need your help - Coderanch server fundraiser https://coderanch.com/t/782867/Coderanch-server-fundraiser