本文共 785 字,大约阅读时间需要 2 分钟。
整数以二进制的形式打印
1.使用stl
#include2.#include #include void printBinary(int n) { std::bitset<32> bits(n); for (int i = bits.size() - 1; i >= 0; --i) { std::cout << bits[i]; } std::cout << std::endl;} int main() { printBinary(10); printBinary(-10); printBinary(23); return 0;}
#include <stdio.h>
#include <stdlib.h>
int
main(
void
)
{
char
buffer[33];
int
a = 0x55555555;
itoa(a, buffer, 2);
printf
(
"%s\n"
, buffer);
return
0;
}
typedef
unsigned
char
BYTE
;
void
print_binary(
BYTE
byte)
{
int
i;
for
(i = 7; i >= 0; --i)
{
printf
(
"%d"
, (byte >> i) & 0x01);
}
}
int
main(
void
)
{
BYTE
a = 0xAA;
print_binary(a);
putchar
(
'\n'
);
return
0;
}
转载地址:http://mofdi.baihongyu.com/