Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re^2: Decimal to Binary using Bitwise and operator

by AchyuthaRao (Initiate)
on Jul 15, 2009 at 20:37 UTC ( #780471=note: print w/ replies, xml ) Need Help??

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

Hi RMGir!
I wrote this program and it gives a correct output.
But I have one thing undone: that to display the binary number based on this output.
Could you please let me know how?

# Program to convert Decimal no. to Binary using Bitwise and operator print "Input a decimal number lesser than 256: ", "\n"; my $decimal; $decimal = <STDIN>; print "The decimal number AND 128 gives us: ", $decimal & 128, "\n"; print "The decimal number AND 64 gives us: ", $decimal & 64, "\n"; print "The decimal number AND 32 gives us: ", $decimal & 32, "\n"; print "The decimal number AND 16 gives us: ", $decimal & 16, "\n"; print "The decimal number AND 8 gives us: ", $decimal & 8, "\n"; print "The decimal number AND 4 gives us: ", $decimal & 4, "\n"; print "The decimal number AND 2 gives us: ", $decimal & 2, "\n\n"; print "The decimal number AND 1 gives us: ", $decimal & 1, "\n";

Comment on Re^2: Decimal to Binary using Bitwise and operator
Download Code
Replies are listed 'Best First'.
Re^3: Decimal to Binary using Bitwise and operator
by RMGir (Prior) on Jul 16, 2009 at 11:49 UTC
    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.

      Thanks a lot for the guidance, Mike!
        #!/usr/bin/perl -w # # 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);

      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).


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2015-11-25 01:58 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (667 votes), past polls