how are integers stored in memory?

亡梦爱人 提交于 2019-12-06 03:47:59
  1. I'd prefer a reinterpret_cast.

  2. Little-endian and big-endian refer to the way bytes, i.e. 8-bit quantities, are stored in memory, not two-decimal quantities. If i had the value 0x12345678, then you could check for 0x78 and 0x12 to determine endianness, since two hex digits correspond to a single byte (on all the hardware that I've programmed for).

There are two different involved concept here:

  1. Numbers are stored in binary format. 8bits represent a byte, integers can use 1,2,4 or even 8 or 1024 bytes depending on the platform they run on.
  2. Endiannes is the order bytes have in memory (less significant first - LE, or most significant first - BE)

Now, 12345678 is a decimal number whose binary (base2) representation is 101111000110000101001110. Not that easy to be checked, mainly because base2 representation doesn't group exactly into one decimal digit. (there is no integer x so that 2x gives 10). Hexadecimal number are easyer to fit: 24=16 and 28=162=256.

So the hexadecimal number 0x12345678 forms the bytes 0x12-0x34-0x56-0x78. Now it's easy to check if the first is 0x12 or 0x78.

(note: the hexadecimal representation of 12345678 is 0x00BC614E, where 0xBC is 188, 0x61 is 97 and 0x4E is 78)

  1. static_cast is one new-style alternative to old-fashioned C-style casts, but it's not appropriate here; reinterpret_cast is for when you're completely changing the data type.

  2. This code simply won't work -- bytes don't hold an even number of decimal digits! The digits of a decimal number don't match up one-to-one with the bytes stored in memory. Decimal 500, for example, could be stored in two bytes as 0x01F4. The "01" stands for 256, and the "F4" is another 244, for a total of 500. You can't say that the "5" from "500" is in either of those two bytes -- there's no direct correspondence.

It should be

unsigned char* p = (unsigned char*)&i;

You cannot use static_cast. Only reinterpret_cast.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!