如何打印(使用cout)数字的方式存储在内存中?

我正在学习关于操作系统的大学课程,我们正在学习如何从二进制转换为hex,十进制到hex等,今天我们刚刚学习如何使用二进制补码(〜数字+ 1)。

我们在纸上做了几个练习,我希望能够在将作品提交给老师之前validation我的答案。 我为前几个练习编写了一个C ++程序,但是现在我被困在了如何用下面的问题来validation我的答案:

char a, b; short c; a = -58; c = -315; b = a >> 3; 

我们需要 abc 内存中显示二进制表示。

我已经在纸上做了,它给了我下面的结果(二进制补码之后的所有二进制表示记忆中的数字):

a = 00111010(这是一个字符,所以1个字节)

b = 00001000(这是一个字符,所以1字节)

c = 11111110 11000101(这是一个简短的,所以2字节)

有没有办法来validation我的答案? 在C ++中是否有一个标准的方式来显示一个数字的内存中的二进制表示,还是我必须自己编码每个步骤(计算二进制补码,然后转换为二进制)? 我知道后者不会这么长,但我很好奇,是否有一个标准的方法来这样做。

最简单的方法可能是创build一个表示值的std::bitset ,然后将其stream式传输到cout

 #include <bitset> ... char a = -58; std::bitset<8> x(a); std::cout << x; short c = -315; std::bitset<16> y(c); std::cout << y; 

使用即时转换为std::bitset 。 没有临时variables,没有循环,没有function,没有macros。

住在Coliru

 #include <iostream> #include <bitset> int main() { int a = -58, b = a>>3, c = -315; std::cout << "a = " << std::bitset<8>(a) << std::endl; std::cout << "b = " << std::bitset<8>(b) << std::endl; std::cout << "c = " << std::bitset<16>(c) << std::endl; } 

打印:

 a = 11000110 b = 11111000 c = 1111111011000101 

如果要显示任何对象的位表示forms,而不仅仅是整数forms,请记住先重新解释为char数组,然后可以将该数组的内容(hex或甚至二进制forms)(通过位集)打印出来:

 #include <iostream> #include <bitset> #include <climits> template<typename T> void show_binrep(const T& a) { const char* beg = reinterpret_cast<const char*>(&a); const char* end = beg + sizeof(a); while(beg != end) std::cout << std::bitset<CHAR_BIT>(*beg++) << ' '; std::cout << '\n'; } int main() { char a, b; short c; a = -58; c = -315; b = a >> 3; show_binrep(a); show_binrep(b); show_binrep(c); float f = 3.14; show_binrep(f); } 

请注意,最常见的系统是little-endian,所以show_binrep(c)的输出不是你所期望的,因为这不是它存储在内存中的方式。 如果你正在寻找二进制表示,那么一个简单的cout << bitset<16>(c)可以工作。

在C ++中是否有标准的方法来显示数字的内存中的二进制表示?

不,没有std::bin ,就像std::hexstd::dec ,但是你自己输出一个数字二进制并不难:

你输出最左边的一位,掩盖所有其他位,左移,并重复所有的位。

(types中的位数是sizeof(T) * CHAR_BIT 。)

类似于已经发布的内容,只需要使用位移和掩码来获取位; 可用于任何types,作为一个模板(只有不知道是否有一个标准的方法来获得1个字节的位数,我在这里使用8)。

 #include<iostream> template<typename T> void printBin(const T& t){ size_t nBytes=sizeof(T); char* rawPtr((char*)(&t)); for(size_t byte=0; byte<nBytes; byte++){ for(size_t bit=0; bit<CHAR_BIT; bit++){ std::cout<<(((rawPtr[byte])>>bit)&1); } } std::cout<<std::endl; }; int main(void){ for(int i=0; i<50; i++){ std::cout<<i<<": "; printBin(i); } } 
 #include <iostream> #include <cmath> // in order to use pow() function using namespace std; string show_binary(unsigned int u, int num_of_bits); int main() { cout << show_binary(128, 8) << endl; // should print 10000000 cout << show_binary(128, 5) << endl; // should print 00000 cout << show_binary(128, 10) << endl; // should print 0010000000 return 0; } string show_binary(unsigned int u, int num_of_bits) { string a = ""; int t = pow(2, num_of_bits); // t is the max number that can be represented for(t; t>0; t = t/2) // t iterates through powers of 2 if(u >= t){ // check if u can be represented by current value of t u -= t; a += "1"; // if so, add a 1 } else { a += "0"; // if not, add a 0 } return a ; // returns string } 

这是得到一个数字的二进制表示的真正方法:

 unsigned int i = *(unsigned int*) &x; 

这是你在找什么?

 std::cout << std::hex << val << std::endl;