Thread: Turning Byte into Binary

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    11

    Turning Byte into Binary

    Hey guys,

    I'm having trouble. I have a byte, and I need to turn it into binary. I know how to mask each individual bit but I don't know how to get that bit. This is what I have so far....

    (pseudo code)

    Logical AND the byte to get each specific bit. To get the left most bit I AND by 0x80 and that would give me the left most bit's value. I would then shift to the right to bring it to the right most bit.

    But I am unsure what to do from here.

    Thanks in advance,
    Lang

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You have one bit. So how do you get the next bit? And the next? (Note: This part almost doesn't change.)
    You do have to remember that you need to work with the original byte, not the byte after it's been anded and shifted.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Search the forums, I have both seen the question asked, and seen it answered (because I have answered it a time or two) before.

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    hmm, I am confused a bit on your question. A byte is really 8 bits, 8 bits in of itself is in binary. For testing printing out a char data type to binary representation you could use a 'printf' statement using the %o specifier.

    If you are simply needing to know if a particular bit is set within the byte, then masking is a viable option to test for that.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    %o is octal, so it's unclear whether that will help OP.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Do you mean you want to turn a byte into a string of 1's & 0's like "10010100"?

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by cpjust View Post
    Do you mean you want to turn a byte into a string of 1's & 0's like "10010100"?
    That was my understanding of the question. If that is the case, please just search the forum, OP. The question has come up from many, and been answered by quite a few.

  8. #8
    Registered User
    Join Date
    Aug 2008
    Posts
    67
    Assuming you want to turn a number into its binary equivalent:

    I've always done it similar to this (probably reinventing the wheel):

    number = 255

    pseudo-code-ish

    Code:
    while (number != 0) {
        buffer = (number % 2) & buffer
        number /= 2
    }
    while ((strlen(buffer) % 8) != 0) // pad with zeros
        buffer = 0 & buffer

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    number % 2
    number /= 2

    using bit operations like & and >> is preffered for bit manipulation
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. error: identifier "byte" is undefined.
    By Hulag in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2003, 05:46 PM