Thread: Using bitshifting to convert to binary.

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    Quote Originally Posted by +Azazel+ View Post
    With >>= it doesnt work at all.
    Wait, no.. you're shifting the mask, not the variable. My mistake.
    It's wrong because you're starting from the wrong side of the number. Start from the high order:
    Code:
    int main()
    {
    	unsigned int  Mask = 0x80;
    	int           count;
    	int           x;
    
    	x = 0;
    	count = 'a';
    	for (x = 0; x < 8; x++)
    	{
    		if (count & Mask)
    			printf("1");
    		else
    			printf("0");
    		Mask >>= 1;
    	}
    }
    Main can just be main(void) since you aren't using any of the command lines. You must include stdio.h and for the sake of gods, use a for loop instead - it looks better.
    Last edited by Elysia; 12-14-2007 at 08:05 AM.
    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.

  2. #17
    Registered User
    Join Date
    Sep 2007
    Posts
    69
    That works, Thank you!

    What if I also want to assign values to the bits, not just extract them. What would be the method to do that?

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by +Azazel+ View Post
    That works, Thank you!

    What if I also want to assign values to the bits, not just extract them. What would be the method to do that?
    What do you mean by "assign values to the bits"? You mean print 0, 64, 32, 0, 0, 0, 0, 1 or some such?

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

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    I believe it means that the OP wants to assign bits to a binary number, such as 0000 0000 becomes 0001 0000.
    In that case, you simply binary OR the appropriate mask. Like 0x80 sets the highest bit (eg 1000 0000).
    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. #20
    Registered User
    Join Date
    Sep 2007
    Posts
    69
    I believe it means that the OP wants to assign bits to a binary number, such as 0000 0000 becomes 0001 0000.
    In that case, you simply binary OR the appropriate mask. Like 0x80 sets the highest bit (eg 1000 0000).
    Yes indeed, that is what I meant. Thank you very much!

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