Thread: Using bitshifting to convert to binary.

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    69

    Using bitshifting to convert to binary.

    Hello,

    Is there an easy way to look at one bit a time of a character, and printf that number? For example, the letter a is 01100001. I would like to look at the letter a, (starting from right or left it doesnt matter) and do a printf accordingly (if its 1 then a 1 and if its a zero then a 0). Then shift one place and look at that number. So in the end when someone inputs the character 'a', I return "01100001".

    Thank you.

  2. #2
    Registered User
    Join Date
    Nov 2007
    Location
    Bangalore, India
    Posts
    24
    You can use a MASK to hide all other bits except LSB and print 1or 0 depending on what the character & MASK evaluates to. You can do this for all bits in the character.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I would typically do this left to right. There are 8 bits, so you can set up a for loop to loop 8 times, starting with X'80' and shifting right each time 1 bit, ending up with X'01' as your mask. ANDing the mask with your byte will give you true or false.

    Todd

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Of course, you can also shift the number of times to push the bit you want to get all the way to the right and use a mask to mask out all other bits, and print the value.
    Code:
    	int n = 127;
    	#define B1000000 128
    	n &= B1000000;
    	n >>= 7;
    	printf("%i", n);
    Last edited by Elysia; 12-12-2007 at 01:06 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Elysia View Post
    #define B1000000 128
    Nah... If you define some constant - you want to make its name "talking"
    Code:
    #define	QUANT_MASK	(0xf)		/* Quantization field mask. */
    Code:
    #define H263P_INTRA_BIT_MASK         (1<<13)
    You do not use define only to say that 128 in binary is 1000000 because any programmer working with bit-shifting will use hex values like 0x40 and will be able to convert it to binary. The define is used to simplify understanding not the binary representation of the constant - but why this constant is used here.
    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

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't know about you, but 1000 0000 looks more readable to me than 128 (or 0x80).
    Especially when it comes to higher numbers.
    Well, it's not necessary, though, it's up to each and everyone.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    I don't know about you, but 1000 0000 looks more readable to me than 128 (or 0x80).
    Especially when it comes to higher numbers.
    Well, it's not necessary, though, it's up to each and everyone.
    So, in your opinion, you know exactly what value 11000000000000000000000000000000 is? And it's easier to read than 0xC0000000?

    Binary numbers are not easier to read, just longer [which in itself makes them HARDER in my opinion, because you loose count of the number of zeros].

    You will learn to convert hex to binary and binary to hex if you work with these sort of things a lot. If you don't, it probably isn't very important which is easier - as they will both need a bit of thinking time.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I think this might be just about as tight as it gets.
    Code:
    #include <stdio.h>
    
    int main (void) {
    	unsigned char j , c = 0xc3 ; 
    	for (j = 0x80 ; j ; j >>= 1 ) { 
    		printf ((c & j) ? "1" : "0" ) ; 
    	}
        return 0;
    }
    Todd

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Or:
    Code:
    #include <stdio.h>
    
    int main (void) {
    	unsigned char j , c = 0xc3 ; 
    	for (j = 0x80 ; j ; j >>= 1 ) { 
    		putchar('0' + !!(c & j)) ; 
    	}
        return 0;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    cool!

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by matsp View Post
    So, in your opinion, you know exactly what value 11000000000000000000000000000000 is? And it's easier to read than 0xC0000000?
    No, of course not... That's kinda hard-ish to read.

    Binary numbers are not easier to read, just longer [which in itself makes them HARDER in my opinion, because you loose count of the number of zeros].

    You will learn to convert hex to binary and binary to hex if you work with these sort of things a lot. If you don't, it probably isn't very important which is easier - as they will both need a bit of thinking time.

    --
    Mats
    Yes, they are, but if I want to logical AND or logical OR or logical XOR with a binary string, say 0000 0010, then I might as well do a define for that, because it's easier to see what I'm actually doing the operation with rather than a decimal number. When the numbers grow too large, I used another strategy.

    Code:
    const int B00000001 = 1;
    const int B00000010 = 1 << 1;
    const int B00000100 = 1 << 2;
    const int B00001000 = 1 << 3;
    const int B00010000 = 1 << 4;
    const int B00100000 = 1 << 5;
    const int B01000000 = 1 << 6;
    const int B10000000 = 1 << 7;
    const int NumBytes1 = 8;
    const int NumBytes2 = 8 * 2;
    const int NumBytes3 = 8 * 3;
    const int NumBytes4 = 8 * 4;
    const int NumBytes5 = 8 * 5;
    const int NumBytes6 = 8 * 6;
    const int NumBytes7 = 8 * 7;
    const int NumBytes8 = 8 * 8;
    So basically, I have one to 8 bytes mask for binary. If I want to do a shift where the mask is on the second byte, I simply do num & (B0001000 << NumBytes2). It may not be the best solution, and of course, it's just a test, but in theory it seems fine.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Sep 2007
    Posts
    69
    Hey, thanks for the suggestions. After reading the tutorial here, and a few other threads I came up with this:

    Code:
    int             main(int argc, char **argv)
    {
      unsigned int  Mask = 0x1;
      int           count;
      int           x;
    
      x = 0;
      count = 'a';
      while (x < 8)
        {
          if (count & Mask)
            printf("1");
          else
            printf("0");
          Mask <<= 1;
          x++;
        }
    }
    Only problem, is it gives me the numbers in inverse order. How can I change that?

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You're shifting the wrong way - you should shift like Mask >>= 1 so.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Sep 2007
    Posts
    69
    With >>= it doesnt work at all.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by +Azazel+ View Post
    With >>= it doesnt work at all.
    Did you start at 0x80 instead of 1?

    Otherwise, post the whole code!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 06-16-2011, 11:59 AM
  2. Convert 10.2 to binary
    By sara.stanley in forum C Programming
    Replies: 20
    Last Post: 02-08-2006, 09:22 AM
  3. Utility to Convert hex to binary
    By learnC in forum C Programming
    Replies: 3
    Last Post: 10-20-2005, 12:09 PM
  4. 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
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM