Size end bytes

Source: Internet
Author: User

1. Concepts

The so-called large-end and small-end refers to the byte sequence of Multi-byte data types (such as INT) stored in the memory. The small end means that the low address bytes store the low data level, and the high address bytes store the high data level. The Large End refers to the high data level and the high data level in the low address bytes. To put it simply, the high level of data stored in low bytes is the big end, and the low level is the small end.

The following is an example to illustrate the different storage sequence of the integer 0x12345678 in large-end and small-end mode.

 

Memory Address Small-end Mode Big end Mode
Zero X 5250 0x78 0x12
Zero X 5251 0x56 0x34
Zero X 5252 0x34 0x56
Zero X 5253 0x12 0x78

 

You may wonder if I made a mistake. But it is true, because 12 is a high data level, and 78 is a low data level.

2. How to judge

The basic principle of determination is to extract the first byte of a Multi-byte data structure. If the byte is a high data level, the machine is a large end. If the byte is a low data level, then the machine is a small terminal. Do not talk nonsense, on the code.

2.1 pointer Conversion

# Nclude <stdio. h> int isbigendian (); int main () {printf ("size of int type: % d bytes. \ n ", sizeof (INT); int endianflag = isbigendian (); printf (" endianflag: % d \ n ", endianflag); If (endianflag) {printf ("this is a big endian machine. \ n ");} else {printf (" This Is Not A Big endian machine. \ n ") ;}return 0 ;}int isbigendian () {int A = 0x12345678; // convert the int forced type to Char single-byte char B = * (char *) & A; // determine the initial storage location, that is, the content of the first byte, if (B = 0x78) {return 0;} return 1 ;}

2.2 Union

#include <stdio.h>int isBigEndian();int main(){printf("Size of int type:%d bytes.\n",sizeof(int));int endianFlag=isBigEndian();printf("endianFlag:%d\n",endianFlag);if (endianFlag){printf("This is a Big Endian machine.\n");}else{printf("This is NOT a Big Endian machine.\n");}return 0;}int isBigEndian()  {union IntCharUnion{  int a;char b;}temp; temp.a = 0x12345678;  if(temp.b == 0x78){return 0;}return 1;}

 

Size end bytes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.