Get the App
SLTechnology News&Howtos  ›  Development  › 

An example Analysis of the Byte order of C _

Shulou Source: shulou.com Published: 2022-06-01 13:26:08 09月26日 Update

This article mainly explains "C/C++ byte order example analysis". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the idea of Xiaobian slowly and deeply to study and learn "C/C++ byte order example analysis" together.

endian

Recently, I have been looking at the memory encoding of redis, which involves byte order-related content. Here's a quick review.

Data is stored in memory in bytes, and if it is single-byte data (such as char, unsigned char, int8), there will be no byte order problem. But multi-byte data (int, float, double) has to consider byte order. There are two types of endian: big endian and small endian.

large endian order

The high byte of data is stored at the low end of the address; the low byte is stored at the high end of the address. The figure shows the memory arrangement of a four-byte integer with a value of 0x12345678 on a big-endian host.

microtelosequence

The high byte of data is stored at the high end of the address; the low byte is stored at the low end of the address. The figure shows the memory arrangement of a four-byte integer with a value of 0x12345678 on a small-endian host.

Host endian and network endian

In addition to host endian, there is network endian. Host endianness is determined by CPU, Intel Core has been tested to be little-endian. The network endian is big endian. Testing endian can be done with a C source code.

#include int main(int argc, char *argv[]) { int i; int x = 0x12345678; for (i = 0; i

< sizeof(int); ++i) { unsigned char *p = ((unsigned char *)(&x)) + i; unsigned char v = *p; printf("%p 0x%d%d\n", p, v>

>4, v & 0xf ); } return 0;}

The first address of integer x is converted into unsigned char* pointer and then shifted forward i units to obtain the address of this sizeof(int) bytes respectively, and then the value on each address is obtained with *, and converted into hexadecimal output through bit operation.

Linux system can get CPU type by instruction:

cat /proc/cpuinfo | grep name | cut -f2 -d: |uniq -c4 Intel(R) Core(TM) i3-2120 CPU@3.30GHz large endian and small endian interconversion

The conversion between big endian and small endian is actually a memory flip. When you know the byte number of an integer or a pointer, you are doing a mirror exchange. Here is an example of a 64-bit integer:

void memrev64(void *p) { unsigned char *x = p, t; t = x[0]; x[0] = x[7]; x[7] = t; t = x[1]; x[1] = x[6]; x[6] = t; t = x[2]; x[2] = x[5]; x[5] = t; t = x[3]; x[3] = x[4]; x[4] = t;} uint64_t intrev64(uint64_t v) { memrev64(&v); return v;}

64-bit integers have 8 bytes, so when converting byte order:

The 0th byte and the 7th byte are exchanged;

The 1st byte and the 6th byte are exchanged;

The 2nd byte and the 5th byte are exchanged;

The third byte and the fourth byte are exchanged;

For 32-bit integers and 16-bit integers, it is even simpler and will not be repeated.

Thank you for reading, the above is the "C/C++ byte order instance analysis" content, after the study of this article, I believe that we have a deeper understanding of C/C++ byte order instance analysis, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Tags: Bytes addresses integers big ends hosts memory data storage instances instance analysis analysis content that is networks problems learning low-order low-end units can be passed through the Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Linux Shulou Technology Shulou Tech Info Apple