Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Decimal to Binary using Bitwise and operator

by AchyuthaRao (Initiate)
on Jul 15, 2009 at 18:54 UTC ( #780435=perlquestion: print w/ replies, xml ) Need Help??
AchyuthaRao has asked for the wisdom of the Perl Monks concerning the following question:

Hi Folks! I am new to Perl programming.

I am currently learning Perl from the book "Beginning Perl" by Simon Cozens.

In one of the "Excercise" questions:

------------------------------------------

Q: Write a program that asks for a Decimal number less than 256 and converts it to Binary. (Hint: You may need to use the bitwise and operator, 8 times)

------------------------------------------

Could you kindly let me know the program? Please note that there were some other methods to solve this problem, but it would be great if you can write the program using the bitwise "and" operator only, as metioned in the question.

Comment on Decimal to Binary using Bitwise and operator
Re: Decimal to Binary using Bitwise and operator
by Corion (Pope) on Jul 15, 2009 at 19:01 UTC

    This is not a homework site and we will not solve your homework problems for you.

    If you show us the code you've already written, and tell us what problems you encounter with it, we will gladly assist you in understanding where your code goes wrong and how to fix it. But we will not write complete programs for you.

    As for the exercise at hand, I'm pretty confident that all you need to solve the exercise is covered in the chapters leading up to and the chapter actually posing the problem, so you will just have to revisit these to solve the exercise.

    As a starting point, consider the case of displaying a whole number between 0 and 1 (inclusive) in binary. As a second step, expand this to displaying a whole number between 0 and 3 in binary. It should be easy to expand your solution to any decimal number range.

      Thanks for the info, Corion!
Re: Decimal to Binary using Bitwise and operator
by zwon (Monsignor) on Jul 15, 2009 at 19:02 UTC
    The point of the question is that you should write this program yourself. Otherwise it doesn't make sense.
      Sorry, zwon. Will try writing programs myself, hereafter and only later post my queries
Re: Decimal to Binary using Bitwise and operator
by RMGir (Prior) on Jul 15, 2009 at 19:12 UTC
    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
      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.


        Mike
      Thank you.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://780435]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (10)
As of 2014-11-26 13:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (171 votes), past polls