When you look at the second section of programming beauty, it's a defined integer and then takes a bit. But his or her operation symbol seems to have been wrong, written in a different or symbolic "^", should be "|". I was suddenly interested in the binary output. Want to know how to output the binary. We know C + + output hexadecimal is cout〈〈hex〈〈a, and octal is cout〈〈ocx〈〈a, binary is not the default output format, need to write their own function to convert, so the Internet search. Online thinking is really extensive AH.
Some methods are listed below.
#include 〈iostream〉 #include 〈list〉 〈bitset〉using #include namespace std;
Recursive output binary function void binaryrecursion (int n) {int A;
a=n%2;
n=n〉〉1;
if (n==0);
else Binaryrecursion (n);
Cout〈〈a;
///Use container to convert binary void binaryvector (int n) {int temp;
Temp=n;
List〈int〉l;
while (temp!=0) {L.push_front (temp%2);
temp=temp〉〉1;
For (List〈int〉::iterator Iter=l.begin (); Iter!=l.end (); iter++) Cout〈〈*iter;
Cout〈〈endl;
}//General method, 32-bit, progressively with 1 to do with operations.
void Binarycout (int n) {for (int i=31;i〉=0;i--) {cout〈〈 ((n〉〉i) &1);
} Cout〈〈endl;
///Use Bitset to convert binary void binarybitset (int n) {cout〈〈bitset〈sizeof (int) *8〉 (n) 〈〈endl;
int main () {int a=1045,b=2;
int C;
C=a+b;
Cout〈〈 "Binaryrecursion (c):";
Binaryrecursion (c);
Cout〈binaryvector (c);
Cout〈〈 "Binarycout (c):";
Binarycout (c);
Cout〈〈 "Binarybitset (c):";
Binarybitset (c);
Cout〈〈 "Binarychar (c):";
Binarychar (c);
Cout〈return 0; }
The results of the run are as follows:
Binaryrecursion (c): 10000010111
Binaryvector (c): 10000010111
Binarycout (c): 0 0000000000000000000010000010111
Binarybitset (c): 00000000000000000000010000010111
BinaryChar (c) : 10000010111 Press any
key to continue
From this we can see that there are two kinds of operations that will result in 32-bit results, not very good. I am not very proficient in C + +, so this efficiency is not very clear which one is good.
I think it might be better to use a container. If it is C, use the character conversion function, or directly after the addition and 1 do with the operation.
Above this C + + several converts the integer to the binary output method summary is small series to share to everybody's content, hoped can give everybody a reference, also hoped that everybody supports the cloud habitat community.