We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,417 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Decimal to Binary Conversion Logic. Help!

This is the Logic for converting a Decimal number to Binary.
I'm having confusion in getting it.
It would be great if someone make me understand this.

for ( long decimal = d ; decimal > 0 ; decimal/=2 )

      {

       binary = decimal%2 + binary  ;

      }

If d=4 then in fisrt go:
4/2=2 and 4%2=0
So, binary = decimal%2 + binary => binary = 0 + (what will be the value of binary here??)

2
Contributors
1
Reply
6 Minutes
Discussion Span
1 Year Ago
Last Updated
3
Views
sadzee
Newbie Poster
5 posts since Mar 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

When trying to understand or debug code, use the println method to print out the values of all the expressions and variables being used it the code so you can see what the computer sees.

One confusion people have when talking about conversion is that everything in the computer is binary. An int value is made of 32 binary digits. The only way to see a "binary" or decimal representation for an int value is to format it into a String. You can not "see" the contents of an int. You can format the int to different Strings to display it in many different bases. The most common is base 10. But it can be formatted to any base.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0541 seconds using 2.65MB