# byte order (byte order) and bit order (bit order)

Source: Internet
Author: User

byte order (byte order) and bit order (bit order)

? In network programming, the network byte order and host order are often mentioned, that is, when an object consists of multiple bytes, it is necessary to pay attention to the order of the objects ' multiple bytes in memory.
Before, I have basically only understood the byte order, but one day when I saw ip.h in the IP head struct struct IPHDR definition, I found that in one byte of the 8 bit also distinguish between the size of the end, then I am confused, not to say that the size of the end only in a number of bytes will be differentiated? , why does the definition here differentiate the size of bits in a single byte?
? Let's take a look at the definition of struct IPHDR, which will explain why we need to differentiate the size end in one byte.

``structIPHDR {#if defined (__little_endian_bitfield)__u8 IHL:4, Version:4;#elif defined (__big_endian_bitfield)__U8 version:4, IHL:4;#Else #Error "please fix <asm/byteorder.h>"#endif __U8 tos;    __be16 Tot_len;    __BE16 ID;    __be16 Frag_off;    __U8 TTL;    __U8 Protocol;    __SUM16 check;    __be32 saddr; __be32 daddr;/*the Options Start here. * /};``
1. byte order
? There are a lot of articles about byte-order, and I'm not going to say too much about byte-order in this article, but I can't completely get out of the byte order because the key part of the post-sequence and the byte-order have some similarity and connection.
? Byte order that is, how many bytes of an object are sorted in memory, for example, if we want to write an 0x12345678 to an address a, how do we store the four bytes in memory last?
? 0x12 This byte value is the most significant byte, which is the highest bit of the integer value (0x12=0x12000000 in this article), 0x78 is the least significant byte.

???????????? Figure 1: Big-endian byte-order
is the big endian, so-called "big endian", that is, the most significant bytes fall on the low address of the byte storage method.

???????????? Figure 2: Small endian
? The small-endian byte order is the byte that the least significant byte falls on the low address.
? 0x12345678=0x12000000 + 0x340000 + 0x5600 + 0x78, so to keep the value of an object constant between the size and end systems, you must ensure that different systems correctly identify the most significant and least significant bytes ( The highest and least significant bytes cannot be identified incorrectly).
? The same byte order 12 34 56 78 is recognized as 0x12345678 (0x12000000 + 0x340000 + 0x5600 + 0x78=0x12345678) in the big-endian machine and is identified as 0x78563412 in the small-order machine (0x12 + 0x3400 + 0x5600 00+ 0x78000000=0x78563412).
? So if you want to be consistent, you must ensure that the system correctly identifies the most significant byte 0x12 and the least significant byte 0x78, then the order of bytes in the small-end system should be 78 56 34 12.
2. Bit order (bit order)
The byte order is a sequential problem between multiple bytes in an object, which is the order of the 8 bits (bit) in a byte. In general, the system's bit order and byte order are consistent.
? A byte is made up of 8 bits, and the 8 bits also have a case of how to sort, with the most efficient bit, the least significant bit, similar to the byte-order.
? Bit Order 1 0 0 1 0 0 1 0 The most effective bit in the big-endian system is 1, the least significant bit is 0, and the byte value is 0x92. In small-end systems, the highest and least significant bits are the opposite of 0, 1, and the byte value is 0x49.
Like the byte order, to keep a byte value constant, the system can correctly identify the highest and least effective bits.
3. BYTE-Order conversion function Ntohl (s), htonl (s)
In socket programming, the network byte-order conversion function Ntohl and htonl are often used to convert the host sequence and the network order (big-endian), and the byte sequence in the system with the small-end of the host sequence is (val=0x12345678). After the htonl conversion, the byte sequence becomes 12 34 56 78:

???????????? Figure 3:htonl function
After the byte-order conversion, I wonder if the bit sequence was converted together?
Why do you have this question, because the previous article shows that the system's bit sequence and byte order is consistent, now the byte sequence has become the small end of the big-endian then the bit sequence should also be converted together. And if the bit sequence does not change, then the value of each byte will change when these bytes are in the target big-endian system, because the same bit sequence recognizes the different byte values in the small-end system.
First of all, from the source of the htonl, Ntohl is really only a byte-order conversion and not the conversion of the bit sequence, then there is a previous socket programming time only called the Ntohl, htonl and other functions and did not call (and the system does not provide) the bit-sequence conversion function, But the final result is correct, and there is no question of the change in the byte value mentioned above.
How is this "magical" thing solved, as if the system itself gives us a "sneak" solution to my concerns.
4. bit send and receive order
? The send and receive order of bits refers to how a bit in a byte is sent and received in a network cable. In Ethernet (Ethernet), it is the sending order from the least significant bit to the most effective bit, which is the least significant bit first sent, reference: frame.
? This rule in Ethernet is a bit strange, because the byte order is sent according to the big-endian order, but the bit sequence is sent in the way of small end sequence, is directly from the Internet to find a picture, the host sequence itself is an end-of-order:

???????????? Figure 4: Bit send, accept
? bit send, receive order to CPU, software is not visible, because our network card will give us to deal with this kind of conversion, at the time of sending in accordance with small order to send bits, in the receiving time will receive the bit sequence into the host's bit sequence, Here is a small end machine that sends an int integer to a big-endian machine:

???????????? Figure 5: Small End--big-endian bit send example
? Because there is no in-depth understanding of the transfer of the NIC to the bit sequence and the reception, there is likely to be a mistake.
? Now answer the question in section 3rd:
• htonl, the Ntohl function is definitely not synchronizing the bit order in a byte, because if the bit sequence is also converted, then the value of this byte is changed, remember Htonl, Ntohl is just a byte-order conversion function. The
• bit sequence is sent as a small end, the first to send is the least effective bit, the last to send is the most effective bit, the receiving end of the network card after receiving the bit sequence according to the host's bit order to receive the "small-endian" bit stream into the host corresponding bit sequence.
? You can assume that there are two functions such as NTOHB, htonb (b for Bit), the network card is converted by the bit sequence, but these two functions are automatically called by the network card, we usually do not pay attention.
• According to the rules, send, receive the time of the bit sequence conversion, then can ensure that the communication between the different machines do not occur I am worried about the change in byte value.
5. bit field of struct
? There is a convention for bit fields: If a bit field is included in the struct of a C language, bit field a always appears in the low-order bit if the bit field A is defined before domain B.
The smallest addressable unit in a computer is a byte, and bit is not addressable, but in order to abstract we can turn the smallest addressable unit of the computer into a bit, that is, we can get a bit bit alone.
? We have the following code:
``#include <stdio.h>structbit_order{unsigned CharA:2B:3C:3;};intMainintargcChar*argv[]) {unsigned CharCH =0x79;structBit_order *ptr = (structBit_order *) &ch;printf("Bit_order->a:%u\n", ptr->a);printf("Bit_order->b:%u\n", ptr->b);printf("Bit_order->c:%u\n", ptr->c);return 0;}``

? We compile and run the code on two machines in Gentoo (Intel Small End Machine), Hu-unix (big-endian machine), and the results are as follows:

[Email protected] V6-dev ~/station \$./bitfiled
Bit_order->a:1
Bit_order->b:6
Bit_order->c:3
Here are the results of Hp-unix operation
#./bitfiled
Bit_order->a:1
Bit_order->b:7
Bit_order->c:1

? Let's analyze the results of Gentoo above:

???????????? Figure 6: Bit field example of a small end machine
It is easy to understand the output of Gentoo above, Hp-unix above:

???????????? Figure 7: Bit field example of a big-endian machine
? From the above output can see the same code in different machines output different results, that is, our code on different platforms can not be directly ported, the cause of this problem is we mentioned earlier about the bit field of a convention, defined in the previous bit field always appear in the low address bit, Because the order of the different platforms is different, the bit fields we define are not converted according to the platform's size and end result in problems. So how to solve this problem is to determine the size of the platform when you define the bit fields in the struct:

``#include <stdio.h>#include <asm/byteorder.h>structbit_order{#if defined (__little_endian_bitfield)    unsigned CharA:2B:3C:3;#elif defined (__big_endian_bitfield)    unsigned CharC:3B:3A:2;#else#error "Please fix <asm/byteorder.h>"#endif};intMainintargcChar*argv[]) {unsigned CharCH =0x79;structBit_order *ptr = (structBit_order *) &ch;printf("Bit_order->a:%u\n", ptr->a);printf("Bit_order->b:%u\n", ptr->b);printf("Bit_order->c:%u\n", ptr->c);return 0;}``

We also explained the question at the beginning of the article about the definition of struct IPHDR.
? Finally, give you a grand introduction of an article, inspired me very much, the text of a lot of knowledge from it: byte order and bit order

byte order (byte order) and bit order (bit order)

Related Keywords:

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.