# Thread: How To Get The Binary Representation Of A Number Without Using Division

1. ## How To Get The Binary Representation Of A Number Without Using Division

Hello All,

how can we print the binary bit representation of a number without division.
there is a way probably we can do it with the use of UNION.

any suggestions ?  2. Yes, you can do it using a union. There are other ways, as well. Is this a quiz or what? 3. ## hello MacGyver,

i need the procedure of doing that as that is required for one of my presentations. it would be great if you could share the two ways.

and ya its not a quiz NEO.  4. I'm not entirely sure how portable it is, but the idea is to have a union of a struct and an integer type of variable. The struct should contain bitfields representing each bit in the variable. You then need to define a function to print out each bit individually.

Portability issues:

• I'm not entirely sure if the bitfields will be properly lined up with no spacing. There was possibly a discussion on this recently, but I'm not entirely sure what was concluded, although I believe this may not be an issue.
• Endianess. This basically means you may have to print all the bits "backwards" on some machines in order for it to make sense. This requires you to know which type of machine you plan on doing this type of code on or attempting to detect the endianess of the machine you are compiling your code on. 5. IF I understand correctly, why not plain old binary operators?
Code:
```int i, j = 0;
int var = 0xFFFFFFFF;
for (; i < sizeof(var); i++)
{
for (; j < 8; j++, mask <<= 1)
{
printf("1");
}
printf(" ");
}
printf("\n");``` 6. You can also use subtraction to do the same work as division. 7. thanks all for all your valuable inputs THANTOS:

how exactly can we get the desired result using subtraction ? 8. Well you have to go back to the defination of multiplication: 5 * x = x + x + x + x + x

So say I have 16 and I want to divide it by 5 but I can't use division directly. I can then do something like:
Code:
```q := 0
r := 0
x := 16
d := 5
while x >= d do
q := q + 1
x := x - d
end while
r := x```
Now q holds 3 and r holds 1. So 16 / 5 is 3 remainder of 1.

If x is negative then you can do the same process by adding instead of subtracting and making a few small changes. 9. Thanks Thantos !!!

can anyone help me out by suggesting a way to do the same using Unions?

thanks in advance ..   11. i was just thinking whether even this one is correct??

Code:
```
#include <stdio.h>
void convert(int n)
{
if(n)
{
convert(n>>1);
printf("1");
else
printf("0");
}
}

int main()
{
int n=34;
convert(n);
return 0;
}``` 12. Code:
```#include <iostream>

int main()
{
int num = 0x1111;

for(int i = sizeof(int)*8-1; i >= 0; i--)
{
std::cout<<(num>>i)%2;

}

std::cin.ignore();
std::cin.get();
}``` 13. This is the C forum, so try not to use cout and other C++ stuff if you can help it. (Or C# stuff for that matter.  )

BTW:
Code:
`sizeof(int)*8`
->
Code:
```#include <limits.h>
sizeof(int)*CHAR_BIT``` 14. Originally Posted by Elysia IF I understand correctly, why not plain old binary operators?
Code:
```int i, j = 0;
int var = 0xFFFFFFFF;
for (; i < sizeof(var); i++)
{
for (; j < 8; j++, mask <<= 1)
{
Isn't this broken ? I mean, there's definitely missing a "else" statement. Plus, you should set j to 0 before entering the most inner-loop.   Popular pages Recent additions 