Thread: Binary

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    6

    Smile Binary

    Hi !!

    I have to write a program that will take decimal integers and convert them into binary values. They should also convert the negative numbers. Can someone please give advice on how I can do this?

    Thank you.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Well, all numbers inside the computer are already stored in binary. I assume for negative numbers you are supposed to print them in two's compliment? So -1 is 1111 1111 1111 1111?

    This code will print the (32 bit) binary representation of any int:

    long i, number, mask = 0x80000000L;
    //set number here

    for (i = 0; i < 32; i++){
    if (number & mask) printf("1");
    else printf("0");
    mask = mask >> 1;
    }

    This will print the number as it is stored in the computer's memory, which is a 32 bit, 2's compliment binary representation.

  3. #3
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    hmm..how come when I wrote my own it was so much larger? *sigh* I just do everything the hard way...

  4. #4
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    Oh, well that's wrong anyway...try using this (it only does 16-bit..for some reason if I go higher it craps out on me..

    Code:
    #define MASK 1
    
    void convert(int dec)
    {   
        int i,moo=dec;
    	int tmp;
    
    	printf("In Binary: ");
        for(i=16; i>=0; i--)
        {
    		dec >>= i;
    		tmp = (dec &MASK);
    		printf("%i",tmp);
    		dec=moo;
        }
    	printf("\n");
    
    }

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    If you are lazy, you could use itoa().
    Code:
    #include <stdlib.h>
    int main()
    {
       int num = 32;
       char str[33];
    
       itoa(num,str,2);
       printf("str:%s\n",str);
       return 0;
    }

  6. #6
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284

    Post

    wouldnt 32 be better replaced by sizeof(long)*CHAR_BIT
    Originally posted by The V.
    Well, all numbers inside the computer are already stored in binary. I assume for negative numbers you are supposed to print them in two's compliment? So -1 is 1111 1111 1111 1111?

    This code will print the (32 bit) binary representation of any int:

    long i, number, mask = 0x80000000L;
    //set number here

    for (i = 0; i < 32; i++){
    if (number & mask) printf("1");
    else printf("0");
    mask = mask >> 1;
    }

    This will print the number as it is stored in the computer's memory, which is a 32 bit, 2's compliment binary representation.

  7. #7
    Unregistered
    Guest
    Code:
    #include <stdio.h>
    int main( void ) {
    	int x,number=231566;
    	for(x=0;x<32;x++) printf("%d", !!(number & (1<<x)));
    	printf("\n");
    	return 0;
    }
    Ah, simplicity.

    Quzah.

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Originally posted by pinko_liberal
    wouldnt 32 be better replaced by sizeof(long)*CHAR_BIT
    You could, but a long is defined as 32 bits, so it's kind of a moot point. It would be a good idea, however, if you wanted to use this with other data types.

    That, though, is the reason I used long, not int, because sizeof(int) is *not* fixed, it can be either 16 or 32. sizeof(long) and sizeof(short) are fixed at 32 and 16 bits.

    And my code DOES work, Ken -- yours, however, won't work quite right -- the highest bit of a 16 bit number is bit 15, not bit 16 -- your loop will execute 17 times, not 16.

    If you have problems with that code, you may have forgotten the "L" at the end of the lask string -- it is important to tell the compiler this is a long literal, otherwise it may treat it as a short literal, and make its value zero.

  9. #9
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Well neither of your codes will work on a big endian machine

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Originally posted by Nick
    Well neither of your codes will work on a big endian machine
    Won't they? I thought that the binary operators worked irregardless of the endian -- it would make too much proting too hard.

    I mean, 0x8000000L always means the MSB is set, because if it didn't, on a little endian you'd need to type 0x00000080L, and, as I've used this on a little endian machine, you don't need to worry about bit order on disk/in memory.

    AFAIK, when specifying a hex literal, it is ALWAYS written with the MSB first.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > You could, but a long is defined as 32 bits, so it's kind of a moot point.
    I think the standard says that longs should be at least 32 bits.

    > Well neither of your codes will work on a big endian machine
    Yes they will - the endianness doesn't matter.

    You have to use tricks to find out what the endianness of a machine is.
    http://www.eskimo.com/~scs/C-faq/q20.9.html
    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.

  12. #12
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    k, fixed it so it does 32-bit..

    Code:
    #include <stdio.h>
    #include <conio.h>
    #define MASK 1
    
    void convert(int dec)
    {
        int i, moo=dec, tmp;
    
    	printf("In Binary: ");
        for(i=31; i>=0; i--)
        {
    		dec >>= i;
    		tmp = (dec &MASK);
    		printf("%i",tmp);
    		dec=moo;
        }
    	printf("\n");
    
    }
    int main(void)
    {
    	int dec;
    
        while(1)
        {
    		printf("Enter a #: ");
        	scanf("%d",&dec);
    		convert(dec);
    	}
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 02:02 PM
  2. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  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. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 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