Concepts about big-size terminals and bit Domains

Source: Internet
Author: User

Size end:

For data types such as char in C ++, it occupies a byte size and will not cause any problems. However, when the numeric type is int, In a 32-bit system, it takes up 4 bytes (32bit). In this case, the order of the four bytes in the register is generated. For example, int maxheight = 0x12345678, & maxheight = 0x0042ffc4. How can I store it? At this time, we need to understand the principle of computer size.

Big end: (big-Endian) Place the high byte of the value on the low address of the memory, and place the value's position byte on the high address of the memory.

Small End: (little-Endian) is to put the high byte of the number on the high address, the low byte on the low address.

The commonly used X86 architecture is the small-end mode, while most DSPs and arm are also the small-end mode, but some arm can choose the large-size-end mode. Therefore, the above maxheight should be stored in the small-end mode. For details, see the following two tables.

Address 0x0042ffc4 0x0042ffc5 0x0042ffc6 0x0042ffc7

Value

0x78

0x56

0x34

0x12

Figure (1) shows the small-end mode. 
Address 0x0042ffc4 0x0042ffc5 0x0042ffc6 0x0042ffc7
Value

0x12

0x34

0x56

0x78

Figure (2) shows the big end mode.

Through the above table, we can see the differences in the size of the end. Here we cannot discuss the better way. I personally think the big end mode is more in line with my habits. (Note: Here I also want to say that there is no data type in the computer memory, such as Char or Int. The role of this type in code is to let the compiler know the number of data records that should be read from that address each time and assign values to the corresponding variables .)

Bit domain:

In a computer, binary 0 and 1 are used to represent data. Each 0 or 1 occupies 1 bit of storage space and 8 bytes constitute a byte ), is the smallest unit of data type in a computer. For example, char occupies one byte in a 32-bit system. However, as we know, sometimes the data in the program may not need such bytes. For example, if the status of a switch is enabled or disabled, it can be expressed by replacing 1 with 0. In this case, only one bucket is required for the switch status. If one byte is used for storage, another 7-bit storage space is obviously wasted. Therefore, in C language, we have the concept of bit segments (some are also called bit domains. The specific syntax is to add the colon (:) and the number of digits of the specified bucket after the variable name. The specific definition syntax is as follows:

 

Struct bit segment name
{
Bit segment data type bit segment variable name: Bit segment length;
.......
}

// Instance
Struct Node
{
Char A: 2;
Double I;
Int C: 4;
} Node;


In fact, the definition is very simple. The meaning of the above example is to define a char variable A, which occupies 2-bit storage space, a double variable I, and an int variable C that occupies 4-bit storage. Note that the variable used byte is changed here. It is not a commonly defined int variable that occupies 4 bytes, and a char variable occupies 1 byte. In the actual running environment, sizeof (node) = 24 is obtained because the memory bytes are aligned. The memory byte alignment has been discussed in the previous article.

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.