1. ## 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. 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++){
else printf("0");
}

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. hmm..how come when I wrote my own it was so much larger? *sigh* I just do everything the hard way...

4. 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;
printf("%i",tmp);
dec=moo;
}
printf("\n");

}```

5. 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. 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++){
else printf("0");
}

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. 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. 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. Well neither of your codes will work on a big endian machine

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

12. k, fixed it so it does 32-bit..

Code:
```#include <stdio.h>
#include <conio.h>

void convert(int dec)
{
int i, moo=dec, tmp;

printf("In Binary: ");
for(i=31; i>=0; i--)
{
dec >>= i;
printf("%i",tmp);
dec=moo;
}
printf("\n");

}
int main(void)
{
int dec;

while(1)
{
printf("Enter a #: ");
scanf("%d",&dec);
convert(dec);
}

return 0;
}```