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 ?
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 ?
Yes, you can do it using a union. There are other ways, as well. Is this a quiz or what?
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.
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.
You can also use subtraction to do the same work as division.
thanks all for all your valuable inputs
THANTOS:
how exactly can we get the desired result using subtraction ?
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:
Now q holds 3 and r holds 1. So 16 / 5 is 3 remainder of 1.Code:q := 0 r := 0 x := 16 d := 5 while x >= d do q := q + 1 x := x - d end while r := x
If x is negative then you can do the same process by adding instead of subtracting and making a few small changes.
Thanks Thantos !!!
can anyone help me out by suggesting a way to do the same using Unions?
thanks in advance ..
MacGyver already has.
i was just thinking whether even this one is correct??
Code:#include <stdio.h> void convert(int n) { int mask=0X000000001; if(n) { convert(n>>1); if(mask & n) printf("1"); else printf("0"); } } int main() { int n=34; convert(n); return 0; }
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(); }
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
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.