how are integers stored in memory?
I'm confused when I was reading an article about Big/Little Endian.
Code goes below:
#include <iostream>
using namespace std;
int i = 12345678;
int main()
{
char *p = (char*)&i; //line-1
if(*p == 78) //line-2
cout << "little endian"开发者_开发知识库 << endl;
if(*p == 12)
cout << "big endian" << endl;
}
Question:
In line-1, can I do the conversion using
static_cast<char*>(&i)
?In line-2, according to the code, if it's little-endian, then
78
is stored in the lowest byte, else12
is stored in the lowest byte. But what I think is that,i = 12345678;
will be stored in memory in binary.If it's little-endian, then the last byte of
i
's binary will be stored in the lowest byte, but what I don't understand is how can it guarantee that the last byte ofi
is78
?Just like, if
i = 123;
, theni
's binary is01111011
, can it guarantee that in little-endian,23
is stored in the lowest byte?
I'd prefer a
reinterpret_cast
.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 value0x12345678
, then you could check for0x78
and0x12
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:
- 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.
- 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)
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.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.
精彩评论