Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Binary Representation of integers

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...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
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
Heroic work plunger man. Please allow me to introduce you to this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic