Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^3: Decimal to Binary using Bitwise and operator

by RMGir (Prior)
on Jul 16, 2009 at 11:49 UTC ( #780666=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Decimal to Binary using Bitwise and operator
in thread Decimal to Binary using Bitwise and operator

That's pretty good!

All you're missing now is a way to string this information together. If $decimal & 128 is true, then you want a "1", otherwise you want a "0". Then you append the same thing for 64, 32, 16, ....

At the end, you'll have your answer.

After that, what's left is looking at your code and seeing if you can clean it up, remove redundancies, convert sequences of similar instructions to loops with the differences moved into constant arrays, things like that.

Oh, and don't forget tests! The nice thing about having such a limited input domain (only 256 possible inputs) is that you can actually test your program for all of them and make sure you got it right.


Mike


Comment on Re^3: Decimal to Binary using Bitwise and operator
Re^4: Decimal to Binary using Bitwise and operator
by AchyuthaRao (Initiate) on Jul 17, 2009 at 09:03 UTC
    Thanks a lot for the guidance, Mike!
      #!/usr/bin/perl -w # decbi.pl # author: Jeff S # date: 11/12/09 # convert a decimal less than 256 to binary print "Please enter the value(less than 256) you wish to convert into a binary number: \n"; $bin = <STDIN>; chomp ($bin); print "The binary value of $bin is: ", "\n"; #use the bitwise & operator to get the binary value: print ((128&$bin) /128); print ((64&$bin) /64); print ((32&$bin) /32); print ((16&$bin) /16); print ((8&$bin) /8); print ((4&$bin) /4); print ((2&$bin) /2); print ((1&$bin) /1);
Re^4: Decimal to Binary using Bitwise and operator
by jhumphreys (Novice) on Oct 04, 2012 at 02:26 UTC

    Mike or other knowledgeable Monk-

    I put a script together, and it gives accurate outputs, but am wondering if one of you could critique it.

    At this typing, I am very new to Perl.

    Best, J.

      Your best bet would be to post your script as a new question, and refer to this post in it (if you're answering this same problem).

      Mike

        Thanks, Mike, will post script as new question.

        J.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://780666]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (15)
As of 2014-12-18 15:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (58 votes), past polls