简体   繁体   中英

How does c++ interpret a binary number

I have a general question about binary numbers in c++. I am reading in a binary file of 32-bit numbers, and then writing these numbers to a text file. My question is, when I do

long int temp;
temp = ( fileBuf[N * 4 * i + 4 * j + 0] << 24 |
         fileBuf[N * 4 * i + 4 * j + 1] << 16 |
         fileBuf[N * 4 * i + 4 * j + 2] << 8  | 
         fileBuf[N * 4 * i + 4 * j + 3] << 0  );
myfile1 << temp << "\t";

does c++ understand that I want it to reinterpret the binary as a decimal number?

All numbers inside of a C++ int variable are binary. The conversion to or from decimal occurs as the number is read or written, or as the compiler converts it from a constant into code.

If I interpret this question as "Will this do what I want it to?" then the answer is yes , as long as what you want it to do is read a 32 bit, big endian integer out of a buffer (presumably loaded from a file) at offset 4 * N * i + 4 * j .

Assuming of course that fileBuf is declared as a character or unsigned character type. It would behave differently if it were, for example, an array of shorts. You'd end up with a mutilated representation of a 64 bit quantity.

实际上,在最后一行打印之前,该数字不会转换为十进制数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM