Little vs Big Endianess: How to interpret the test

前端 未结 6 962
长发绾君心
长发绾君心 2020-12-11 03:44

So I\'m writing a program to test the endianess of a machine and print it. I understand the difference between little and big endian, however, from what I\'ve found online,

6条回答
  •  悲&欢浪女
    2020-12-11 03:55

    Yes, that answers the question. Here's a more general answer:

    #include 
    #include   
    #include 
    
    using namespace std;
    
    int main()
    {
    cout<<"sizeof(char) = "<

    This displays 8 4 2 1 for the dereferenced values at c, c+1, c+2, and c+3 respectively. The sum y is 16909320, which is equal to x. Even though the significance of the digits grow from right-to-left, this is still Little Endian because the corresponding memory values also grow from right-to-left, which is why the left-shift binary operator << would increase a variable's value until non-zero digits are shifted off the variable altogether. Don't confuse this operator with std::cout's << operator. If this were Big Endian, then the display for c, c+1, c+2, and c+3 respectively would look like: 1 2 4 8

提交回复
热议问题