# Converting Integers to Binary

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-03-2002
cprog
Converting Integers to Binary
Hi Im a newbee at this and Im in a bit of trouble converting Integers to Binary numbers I know I have to use a function but I have no idea how to set up this function so that it could do this thanx in adavance
• 10-03-2002
Powerfull Army
Use a cast.Im quite rusty and a newbie to begin with but I believe you want this(this is an example):

int i=1;
bool b;
b=<bool>i;

I believe that should work,and all types have casts:

char g="T"
int ASCII;
ASCII=<int>g;

int f=104;
float t;
t=<float>f;

• 10-03-2002
Dave_Sinkula
The following is one way to do it.
Code:

```#include <stdio.h> #include <limits.h> #define WYSIWYG(x)  #x, (x) char *bits_uint(char *dest, unsigned int value) {   char *start = dest;   if(dest)   {       unsigned int mask;       for(mask = (-1U >> 1) + 1; mask; mask >>= 1)       {         *dest++ = value & mask ? '1' : '0';       }       *dest = 0;   }   return(start); } int main(void) {   unsigned int value = 125;   char binary[sizeof(value) * CHAR_BIT + 1];   bits_uint(binary, value);   printf("%s = %u = %X\n", WYSIWYG(value), value);   printf("%s = \"%s\"\n", WYSIWYG(binary));   return(0); } /* my output value = 125 = 7D binary = "00000000000000000000000001111101" */```
There is a bit going on there; I hope you ask questions on anything that does not make sense. I guess I'm trying to say I hope this isn't just a homework question that you'll take the code and run with.
• 10-03-2002
cprog
Thanx
thanx for the help will be usefull thanx gone from completely lost to having an idea were to start
• 10-03-2002
Cshot
Your integer is already stored in binary format. If you simply want to print it out and not store it in a string, you can likewise do this:

Code:

```#include <stdio.h> int main() {   int i;   int value = 125;   for(i = 0; i < 32; i++)       printf("%d", (value >> (31 - i)) & 1);   printf("\n");   return 0; }```
• 10-03-2002
Monster
He Cshot,

You should use the sizeof operator to make your code more portable.

Cheers,
Monster
• 10-03-2002
Powerfull Army
O,I thought you meant you wanted integer to bool,sory.:o
• 10-03-2002
Cshot
>> You should use the sizeof operator to make your code more portable.
Yea I should've. I was just demonstrating the concept and didn't want to confuse him.

Here's with sizeof:
Code:

```#include <stdio.h> int main() {   int i;   int value = 125;   for(i = 0; i < sizeof(int)*8; i++)       printf("%d", (value >> (sizeof(int)*8 - i)) & 1);   printf("\n");   return 0; }```
• 10-03-2002
Dave_Sinkula
Cshot's code does not appear to be correct.
Code:

```#include <stdio.h> int main() {   int i;   int value = 125;   for(i = 0; i < sizeof(int)*8; i++)       printf("%d", (value >> (sizeof(int)*8 /* - 1 */ - i)) & 1);   printf("\n");   return 0; } /* my output 10000000000000000000000000111110 */```
I would like to point out that a char does not necessarily contain 8 bits, so the 8 is not very portable.

Also, an integer /may/ include padding bits, so using sizeof(int) * CHAR_BIT is not even fully portable.

Using unsigned types would be preferable when using bit shifts.

And as an individual preference, I like to do sizeof(value) instead of sizeof(int) -- I try to avoid the middleman of assuming the underlying type.
• 10-03-2002
Cshot
Missed the extra -1 in this line:
printf("%d", (value >> (sizeof(int)*8 - i)) & 1);
No sleep = lots of mistakes :(

>> Also, an integer /may/ include padding bits, so using sizeof(int) * CHAR_BIT is not even fully portable.
• 10-03-2002
Dave_Sinkula
Quote:

> Also, an integer /may/ include padding bits
Huh?
An int has to be an integral multiple of the number of bits in a char, since sizeof(char) is by definition 1, and all sizeof values are ints
Got any references?
While an int must be an integral multiple of the number of bits in a char, not all bits are required to represent a value. (I think having an additional parity bit is an example I have heard of.) The exception is an unsigned char. Sorry, I don't have a reference right now.

[EDIT]
Reference: ISO/IEC 9899-1999 (C99)
Quote:

6.2.6.2 Integer types
1 For unsigned integer types other than unsigned char, the bits of the object representation shall be divided into two groups: value bits and padding bits (there need not be any of the latter). If there are N value bits, each bit shall represent a different power of 2 between 1 and 2N - 1, so that objects of that type shall be capable of representing values from 0 to 2N - 1 using a pure binary representation; this shall be known as the value representation. The values of any padding bits are unspecified.44)

44) Some combinations of padding bits might generate trap representations, for example, if one padding bit is a parity bit. Regardless, no arithmetic operation on valid values can generate a trap representation other than as part of an exceptional condition such as an overflow, and this cannot occur with unsigned types. All other combinations of padding bits are alternative object representations of the value specified by the value bits.

[/EDIT]
• 10-03-2002
swoopy
I actually like CShot's orginal version. If the integer is shorter than 32, it will just add some extra zeros on the front.
• 10-03-2002
swoopy
Quote:

I would like to point out that a char does not necessarily contain 8 bits, so the 8 is not very portable.
sizeof() returns the size in bytes. And if you multiply by 8 you get the number of bits.
• 10-03-2002
Dave_Sinkula
Quote:

I actually like CShot's orginal version. If the integer is shorter than 32, it will just add some extra zeros on the front.
And if it's bigger than 32, it'll be wrong.
Quote:

sizeof() returns the size in bytes. And if you multiply by 8 you get the number of bits.
No. In C, a byte does not necessarily contain 8 bits; a byte contains CHAR_BIT bits, and CHAR_BIT might not be 8.
• 10-03-2002
Cshot
>> No. In C, a byte does not necessarily contain 8 bits; a byte contains CHAR_BIT bits, and CHAR_BIT might not be 8.
I was just wondering if you've seen examples where a CHAR_BIT wasn't 8. However, I think I've seen a word contain 14 bits before.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last