Many friends can't distinguish between "byte order" and "bit order"~

Reprinted from: Many friends can't distinguish between "byte order" and "bit order"~

Many friends can't distinguish between "byte order" and "bit order"~

yesterday

The following article comes from the last bug, the author bug bacteria

1. Parse byte order   

    1) In fact, many small partners should be very familiar with the byte order. Usually, we also call it "size end". However, some small partners may simply know its concept and do not have an in-depth understanding in the actual code development. The author analyzes this problem in detail and paves the way for the later bit order.

    2) Since it is called "byte order", it describes the byte order problem. If there is no concept of byte order for a single byte, however, for the data types of multiple bytes, the byte order arrangement in memory is also different due to machine differences. Then we need to take it into account when considering the portability of the code!

    3) Byte order is also called large and small end mode, which are big endian mode and small endian mode respectively. Simple expression: large end mode - high byte storage and low address; Small end mode - high byte high address; The following describes the distribution of different modes in memory with four byte shaping data 0x78563412 as an example:

     Some small partners are prone to such misunderstandings: they don't seem to find such a problem when moving the pointer in the above figure. How can there be a difference when the pointer in the above figure moves to the address data? The reason is that when you use pointer + +, the next address is sizeof (pointer type), rather than the address + 1 in the figure above.

     Here, the author has also compiled a simple program for your reference:

#include <stdio.h>

#include <stdlib.h>

/*****************************************

 * Fuction:main

 * Author :(Official account: last bug)

 ****************************************/

int main(int argc, char *argv[]) {

    printf("int_Size: %d\n",sizeof(int));

    int Val = 0x78563412;

    int *ptr_int = &Val;

    unsigned char *ptr_char = (unsigned char *)&Val;

    printf("ptr_int  Addr:0X%X\n",ptr_int);

    printf("ptr_char Addr:0X%X\n",ptr_char);



    printf("ptr_char + 0:0X%X = 0X%X\n",ptr_char + 0,*(ptr_char + 0));

    printf("ptr_char + 1:0X%X = 0X%X\n",ptr_char + 1,*(ptr_char + 1));

    printf("ptr_char + 2:0X%X = 0X%X\n",ptr_char + 2,*(ptr_char + 2));

    printf("ptr_char + 3:0X%X = 0X%X\n",ptr_char + 3,*(ptr_char + 3));



    //Used to determine byte order

    if((*(ptr_char))== 0x12)

    {

        printf("Little_Endian\n");

    }

    else

    {

        printf("Big_Endian\n");

    }

    printf("official account:the last one bug!");

   return 0;

}

    The following is the output result. For the big end mode, you can also refer to the above code for simple test.

    4) Most of our current PC s are X86 architecture, basically small end mode. In the embedded system, the chip architectures are different. When developing, we should pay attention to the problems of data storage and reading (such as the access of conjoined data). For the algorithms that are sensitive to the large and small ends, we can also write corresponding conversion algorithms through the characteristics of the large and small ends for corresponding processing.

2. Parsing bit order   

    1) you should be familiar with the byte order, but some partners may not care much about the bit order. On the one hand, the minimum addressing unit is byte. On the other hand, the libraries are well encapsulated. Basically, the interfaces for you are at the level of operating bytes, If we want to go deep into more primitive signals, some small partners may also need to learn relevant knowledge.

    2) We all know that most communications are transmitted by bit signals at the level of "0101...". We need to define the bit sequence from the sender to the receiver whether the high bit(MSB) is transmitted first or the low bit(LSB) is transmitted first.

    3) For example, if we need to use the IO port to simulate the serial port or IIC communication, then I must understand the physical layer data transmission format of the serial port or IIC communication. At this time, we need to pay special attention to the bit sequence. As shown in the figure below, the bit sequence of UART communication is just opposite to that of IIC, which we need to pay attention to when we conduct simulation programming.

     Some friends will ask that I don't simulate these communications at ordinary times, and the bottom layer has done well with me. Basically, I can get byte to use it. It doesn't matter if I don't understand it. The author thinks that these knowledge points are not very difficult. You can learn that there will be more ideas to solve problems in the future. For example, I can't communicate with other devices or If the communication is abnormal, how can we eliminate the problem?

    4) For general network communication, the bottom layer will convert the bit order with you, but there will be some situations that require you to convert the bit order accordingly, which will be determined according to the actual situation. Finally, here is a bit order conversion algorithm you usually use: (as shown in the figure below)

     The algorithm reference code is as follows:

#include <stdio.h>

#include <stdlib.h>

/*****************************************

 * Fuction:Bit_Inverter

 * Author :(Official account: last bug)

 ****************************************/

unsigned char Bit_Inverter(unsigned char Val)

{

    Val = ((Val & 0xAA) >> 1) | ((Val & 0x55) << 1);

    Val = ((Val & 0xCC) >> 2) | ((Val & 0x33) << 2);

    Val = ((Val & 0xF0) >> 4) | ((Val & 0x0F) << 4);

    return Val;

}

/*****************************************

 * Fuction:main

 * Author :(Official account: last bug)

 ****************************************/

int main(int argc, char *argv[]) {

    int i = 0;

    unsigned char test = 0x95;

    unsigned char str1[10] ={0};

    unsigned char str2[10] ={0};

    itoa(test, str1, 2);

    itoa(Bit_Inverter(test), str2, 2);

    printf("%s <---inv---> %s\n", str1,str2);

    printf("official account:the last one bug!");

    return 0;

}

     The operation results are as follows:

3. Final summary   

    Well, today's knowledge sharing is here. Ten thousand tall buildings rise from the ground. Although these little knowledge is not so tall, from personal experience: everything is a process from quantitative change to qualitative change. As long as it can accumulate over time, it will accumulate a lot!

Source: the last bug. Copyright belongs to the original author~

most   after  

Keywords: C C++

Added by TheAngst on Tue, 14 Sep 2021 01:48:52 +0300