Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to change a number into a binary number in c++
How to change a number into a binary number in c++
at present, an integer in C/C++ is 4 bytes and 32 bits. No matter the number entered in octal, decimal or hexadecimal, it has become a binary number in memory, so we can just try to print it directly. Now, taking the 4-byte int type as an example, the C program is as follows:

# include "stdafx.h"

# include "stdio.h"//

# include "iomanip.h"/

void main (void) {

int ndec;

unsigned i;

cout < < "Please enter a decimal integer ... \ nThe integer entered is:";

cin > > nDec;

cout < < "The corresponding binary is:";

if(! nDec) cout < < '';

else{

for(i=x8; ! (i& nDec); i> > =1);

for(; i; i> > =1)

cout < < (i& nDec || );

}

cout < < endl;

}