I'm trying to write two functions (three including main) that together, will output the binary versions of numbers 0-10. It seems simple, but the problem is that one of the functions needs to grab each individual bit of a number (1-10, and 16 bits) and the other needs to put those bits together to form a string. The string contains the binary version of x, where x is any number 1-10.
From the linked code.. To get the each bit of a number, I'm using the function int bits, which grabs the bits in order, and I'm trying to connect them with bitstring, but as you can see, I've got no clue how to do that. I'm working on that for loop too.
Anyone have any ideas? Specifically, how I can put the 'bits' function inside of 'bitstring' and transform it into a string?
Output is supposed to be:
[spoiler]
0 is: 0000000000000000
1 is: 0000000000000001
2 is: 0000000000000010
3 is: 0000000000000011
4 is: 0000000000000100
5 is: 0000000000000101
6 is: 0000000000000110
7 is: 0000000000000111
8 is: 0000000000001000
9 is: 0000000000001001
10 is: 0000000000001010
[/spoiler]
I'll clarify if anything is unclear too. Any help is appreciated.
-
int main() { int n,x,a, c, k; cout<<"Enter an integer in decimal number system"; cin>>x; n=x; cout<<"Binary Value OF Given Number Is: "; for( a=1;n!=0;a++) { n=n/2; } a=a-2; for (c = a; c >= 0; c--) { k = x >> c; if (k & 1) cout<<"1"; else cout<<"0"; } return 0; }