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

Mike
• 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, ....

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!

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.

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

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

Create A New User
Node Status?
node history
Node Type: note [id://780444]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2018-08-17 18:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Asked to put a square peg in a round hole, I would:

Results (182 votes). Check out past polls.

Notices?