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. :)
Printable View
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. :)
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.
hmm..how come when I wrote my own it was so much larger? *sigh* I just do everything the hard way...
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");
}
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;
}
wouldnt 32 be better replaced by sizeof(long)*CHAR_BIT
Quote:
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.
Ah, simplicity.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;
}
Quzah.
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.Quote:
Originally posted by pinko_liberal
wouldnt 32 be better replaced by sizeof(long)*CHAR_BIT
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.
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.Quote:
Originally posted by Nick
Well neither of your codes will work on a big endian machine :)
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.
> 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
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;
}