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

Re: Decimal to Binary using Bitwise and operator

by RMGir (Prior)
on Jul 15, 2009 at 19:12 UTC ( #780444=note: print w/replies, xml ) Need Help??

in reply to Decimal to Binary using Bitwise and operator

I'm not going to give you the program, but I'll give you some hints that may help you figure out the answer.

In the binary representation of a number, a value is made up of a certain number of bits. For values less than 256, as in your question, this requires 8 bits (also known as 1 byte).

Binary numbers are just like our usual decimal numbers - all that changes is the number base. 2 for binary, 10 for decimal.

So, in decimal, 250 is 2*100 + 5*10 + 0*1, while the same number in binary is 11111010 (1 * 128 + 1*64 + 1*32 + 1*16 + 1*8 + 0*4 + 1*2 + 0*1).

The bitwise and operator & works on the bits that make up a number. So 250&32 is going to return 32, because that bit is on in 250, but 250&1 is going to return 0, because the 1's bit is not on in 250.

So, by using & with masks 1, 2, 4, 8, 16, 32, 64, and 128, you should be able to build up the binary representation of your input number...

As a bonus feature, perl's printf implementation allows the "%b" specifier to be used, so you can use that to quickly implement tests for your & implementation.

  • Comment on Re: Decimal to Binary using Bitwise and operator

Replies are listed 'Best First'.
Re^2: Decimal to Binary using Bitwise and operator
by AchyuthaRao (Initiate) on Jul 15, 2009 at 20:37 UTC
    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";
      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!

        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.

Re^2: Decimal to Binary using Bitwise and operator
by Anonymous Monk on Jul 30, 2013 at 22:03 UTC
    Thank you.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://780444]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2017-07-25 01:52 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (362 votes). Check out past polls.