converting variable to bits?

This is a discussion on converting variable to bits? within the C Programming forums, part of the General Programming Boards category; hi... let's say i got a char a = 'A'; and an arry of bool b[8]; how to convert variable ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    39

    Question converting variable to bits?

    hi...

    let's say i got a char a = 'A';
    and an arry of bool b[8];

    how to convert variable a into binary and store it into b[]?

    Thanks

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    A for loop, and use of the >>(shift-right) and &(bitwise-and) operators
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    39
    can you demostrate with some coding?

  4. #4
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>
    
    void printBits(unsigned long val){
        int i, end = sizeof(unsigned long) * 8;
        unsigned long tmp = (ULONG_MAX / 2) + 1;
        for (i=0; i<end; i++){
            if (tmp & val) putc('1', stdout);
            else putc('0', stdout);
            tmp >>= 1;
        }
    }
    
    int main(){
        int number = 0;
        char buf[BUFSIZ];
    
        while (1){
            printf("Please enter a number to convert to binary: ");
            fgets(buf, BUFSIZ, stdin);
            rewind(stdin);
            number = atoi(buf);
            printBits(number);
            printf("\n");
        }
        return 0;
    
    }

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    For example:
    num & 1 is true if the 1st bit is 1.
    Of course if it isnt true, then the 1st bit is 0.
    num = num >> 1 would then bitwise shift num such that the 2nd bit is now the 1st bit.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Excellent code, mitakeet. Except I don't see BUFSIZ declared anywhere. Or is it part of one of the header files?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    39

    Question

    ok.. my code:
    Code:
    void main()
    {
    	unsigned char a = 1;
    	unsigned char b[8];
    	char mask = 1;
    	int i;
    	
    	for(i = 8; i > 0; i--)
    	{
    		printf("%d", a & mask);
    		b[i - 1] = a & mask;
    		a >>= 1;
    	}
    
    	printf("\n");
    
    	for(i = 0; i < 8; i++)
    	{
    		if(b[i] == 1)
    		{
    			printf("1");
    		}
    		else
    		{
    			printf("0");
    		}
    	}
    }
    ok... now how do we convert from bits back to the original variable value?

  8. #8
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    BUFSIZ is declared in <stdio.h> and is typically around 512 bytes.

    If you want to put it back together, you |= with the appropriate bit mask.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  9. #9
    Registered User
    Join Date
    Jul 2005
    Posts
    39
    coding example please....

  10. #10
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    int value = 0;
    int mask1 = 1;
    value |= mask1;//now first (lowest order) bit is set

    int mask2 = 2
    value |= mask2;//now first and second low order bits are set

    int mask3 = 4;....

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  11. #11
    Registered User
    Join Date
    Jul 2005
    Posts
    39

    Question

    based on my program the bits is store on b[8];
    i try:
    Code:
    void main()
    {
    	unsigned char a = 'a';
    	unsigned char c = 0;
    	unsigned char b[8];
    	char mask = 1;
    	int i;
    	
    	for(i = 8; i > 0; i--)
    	{
    		printf("%d", a & mask);
    		b[i - 1] = a & mask;
    		a >>= 1;
    	}
    
    	printf("\n");
    
    	for(i = 0; i < 8; i++)
    	{
    		c |= b[i];
    		c <<= 1;
    
    		if(b[i] == 1)
    		{
    			printf("1");
    		}
    		else
    		{
    			printf("0");
    		}		
    	}
    
    	printf("\n%c\n", c);
    }
    but not working......
    Last edited by draggy; 07-12-2005 at 12:53 PM.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    What does it output?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Jul 2005
    Posts
    39
    output:

    Code:
    10000110
    01100001
    ┬
    Thanks

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    01100001 is the ASCII code for 'a'.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Registered User
    Join Date
    Jul 2005
    Posts
    39
    Quote Originally Posted by dwks
    01100001 is the ASCII code for 'a'.
    yes... the binary ASCII

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 03:35 AM
  2. Having trouble converting a variable
    By KasMage in forum C++ Programming
    Replies: 6
    Last Post: 07-14-2008, 03:43 PM
  3. variable being reset
    By FoodDude in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2005, 12:30 PM
  4. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  5. Writing binary data to a file (bits).
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 03:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21