• 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 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

Converting binary to decimal

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never mind guys, i think i got on the right track and can solve this on my own. Thanks for all the help though!!
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course if your tutor has not advised against it
 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Eliminate 95% of the weeds in your lawn by mowing 3 inches or higher. Then plant tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic