This week's book giveaway is in the Kotlin forum.We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Converting binary to decimal

abduaziz alharbi
Greenhorn
Posts: 4
Hello guys i have a code but need a few changes to make it the way i want, first of all i will show you the code

However, the code i want is one that would make me enter any binary number i want instead of the few i have in the code, for example i want to it to look like this: enter the binary number ( i enter "XXXX")
"XXXX" converted to decimal equals WWWW.

Now i want you to keep in mind that i am a beginner so if you are trying to point out my mistakes please state in what line they are and what should i put instead of what i originally have.

Thanks alot

Liutauras Vilda
Marshal
Posts: 4640
316
• 1
Welcome to the ranch.

If I understand you correctly, you want to get binary number as an input from user.
For that, you'll need scanner, check this (<- link).

However, I don't like the idea, that half of your calculations are not worthwile. I have in mind line 12, when temp is 0.

Also, I'm not sure how your program would behave by passing to a method binary number 10000000000, which is 1024 in decimal. int bounds are -2,147,483,648 to 2,147,483,647.

I'd probably try to think about the alternative solution. Maybe with strings? I might wrong. Let's see what others think.

abduaziz alharbi
Greenhorn
Posts: 4
ops i forgot to mention that i only need 4 digits binary although i do agree that the calculations do suck, if you have any advice towards fixing them i would appreciate it.

abduaziz alharbi
Greenhorn
Posts: 4
Never mind guys, i think i got on the right track and can solve this on my own. Thanks for all the help though!!

Winston Gutkowski
Bartender
Posts: 10573
65
abduaziz alharbi wrote:Never mind guys, i think i got on the right track and can solve this on my own. Thanks for all the help though!!

Well done.

One thing to bear in mind when you're programming is that there's a HUGE difference between what something is, and how it's displayed - and in your case input as well.

Absolutely everything in a computer IS binary; but that's not necessarily how we want to look at it.

HIH

Winston

salvin francis
Bartender
Posts: 1601
35
Just fyi,
http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt%28java.lang.String,%20int%29

The parseInt method in Integer allows you to give a radix.

parseInt("1100110", 2) returns 102

abduaziz alharbi
Greenhorn
Posts: 4
salvin francis wrote:Just fyi,
http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt%28java.lang.String,%20int%29

The parseInt method in Integer allows you to give a radix.

parseInt("1100110", 2) returns 102

can i use this parseInt method in assigments? becuase my code is so much more complicated after doing all the calculations but with this it couldve been so much easier.

salvin francis
Bartender
Posts: 1601
35

Tim Holloway
Bartender
Posts: 18709
71
Since we're on an academic thread, I'll abuse (yet again) my tendency to be pedantic.

The term "digits binary" here is confusing. A binary digit, is called a "bit". But one of the examples was a 6-bit number, so I'm guessing that "4 digits" binary" actually meant that the output number is required to be no more than 4 decimal digits. That is, "9999". Although domain and range are terms taught in calculus, they're usually important to keep in mind when working with computers as well (in fact, rigorous domain/range control is one of the distinctive features of the Ada language).

Mathematically, a 4-digit number abcd is the equivalient of a*1000+b*100*c*10+d, or more precisely a*(10^3)+b*(10^2)+c*(10*1)+d*(10^0). A 4-bit binary number follows the same structure: a*(2^3)+b*(2^2)+c*(2^1)+d*(2^0).

One of the interesting things about this is that the logical bit-shift operator can produce those exponents very easily: 2^3 = 2 << 3, 2^2 = 2 << 2, and so forth. Note that signed/unsigned concerns may come in when larger values are being processed, but they don't factor in for this assignment.

If you're clever, you can see how to leverage this ability using a general-purpose add-and-shift loop to get the internal binary value from its character binary external representation. This is a lot faster than using an explicit power function, although in a pure mathematical sensse, the power function is the less kludgy of the two approaches.

Fun fact: the shift-and-operator power series technique is at the heart of a whole lot of useful computer functions, such as encryption. So specialized hardware is often used. General-purpose CPUs often employ what's known as a "barrel shifter", which can shift numbers multiple places in a single instruction cycle. Telecommunications peripherals often shift large sequences of bits using special hardware such as gate arrays.

Winston Gutkowski
Bartender
Posts: 10573
65
Tim Holloway wrote:2^3 = 2 << 3, 2^2 = 2 << 2, and so forth...

Erm...hate to burst your bubble, but
2^3 == 1 << 3

Winston

Tim Holloway
Bartender
Posts: 18709
71
Oops.

2^3 = 1 << 3,
2^2 = 1 << 2
2^1 = 1 << 1
And of course
2^0 = 1 << 0 = 1

Got my bases and bits confused there,

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.