Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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";

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.


    Mike
      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);

      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
      print "Input a decimal number lesser than 256: ", "\n"; my $decimal; $decimal = <STDIN>; print "The decimal number AND 128 gives us: ", ($decimal & 128)>1, "\n +"; print "The decimal number AND 64 gives us: ", ($decimal & 64)>1, "\n"; print "The decimal number AND 32 gives us: ", ($decimal & 32)>1, "\n"; print "The decimal number AND 16 gives us: ", ($decimal & 16)>1, "\n"; print "The decimal number AND 8 gives us: ", ($decimal & 8)>1, "\n"; print "The decimal number AND 4 gives us: ", ($decimal & 4)>1, "\n"; print "The decimal number AND 2 gives us: ", ($decimal & 2)>1, "\n\n"; print "The decimal number AND 1 gives us: ", $decimal & 1, "\n";

      2017-09-02 Athanasius added code tags

        Anonymous Monk:

        You can save yourself some cut & paste if you do it like:

        $ cat pm_1198573.pl #!env perl use strict; use warnings; print "Input a decimal number between 0 and 255: "; my $decimal = <STDIN> + 0; die "Please follow directions!" if $decimal < 0 or $decimal > 255; print "The number $decimal AND ", (1<<$_), " gives us: ", ($decimal & +1<<$_), "\n" for reverse 0 .. 7; $ perl pm_1198573.pl Input a decimal number between 0 and 255: 113 The number 113 AND 128 gives us: 0 The number 113 AND 64 gives us: 64 The number 113 AND 32 gives us: 32 The number 113 AND 16 gives us: 16 The number 113 AND 8 gives us: 0 The number 113 AND 4 gives us: 0 The number 113 AND 2 gives us: 0 The number 113 AND 1 gives us: 1

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://780471]
help
Chatterbox?
[erix]: I think it depends on the server
[erix]: hmm...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2017-11-18 21:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (278 votes). Check out past polls.

    Notices?