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 ( [id://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

Replies are listed 'Best First'.
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.

Re^4: Decimal to Binary using Bitwise and operator
by Anonymous Monk on Sep 02, 2017 at 11:00 UTC
    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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://780666]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-03-19 06:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found