In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how C language gets each byte of integer". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. let's go deep into the train of thought of the editor. Let's study and learn how C language gets each byte of integer.
Method 1: structure & Joint # include
Typedef unsigned int uint32_t
Typedef unsigned char uint8_t
Union bit32_data
{
Uint32_t data
Struct
{
Uint8_t byte0
Uint8_t byte1
Uint8_t byte2
Uint8_t byte3
} byte
}
Int main (void)
{
Union bit32_data num
Num.data = 0x12345678
Printf ("byte0 = 0x%x\ n", num.byte.byte0)
Printf ("byte1 = 0x%x\ n", num.byte.byte1)
Printf ("byte2 = 0x%x\ n", num.byte.byte2)
Printf ("byte3 = 0x%x\ n", num.byte.byte3)
Return 0
}
Running result:
The disadvantage of this method is that it will be affected by the size of the end. It can only be used slightly if the size of the end is clear. The above is the result of running in small-end mode. The structure-federation method is not only useful in obtaining each byte of data, but also useful in data protocol, data bit splitting, etc., and then share it in more detail. Method 2: shift operation # include
# define GET_LOW_BYTE0 (x) ((x > > 0) & 0x000000ff) / * get the 0th byte * /
# define GET_LOW_BYTE1 (x) ((x > 8) & 0x000000ff) / * get the first byte * /
# define GET_LOW_BYTE2 (x) ((x > > 16) & 0x000000ff) / * get the second byte * /
# define GET_LOW_BYTE3 (x) ((x > 24) & 0x000000ff) / * get the third byte * /
Int main (void)
{
Unsigned int a = 0x12345678
Printf ("byte0 = 0x%x\ n", GET_LOW_BYTE0 (a))
Printf ("byte1 = 0x%x\ n", GET_LOW_BYTE1 (a))
Printf ("byte2 = 0x%x\ n", GET_LOW_BYTE2 (a))
Printf ("byte3 = 0x%x\ n", GET_LOW_BYTE3 (a))
Return 0
}
Running result:
This is also the most common and effective way to get each byte of data. This similar bit operation is widely used in embedded systems, such as representing pixel color values in LCD operations, FLASH operations, and so on. Thank you for your reading, the above is the content of "how C language gets each byte of integers". After the study of this article, I believe you have a deeper understanding of how C language gets each byte of integers. The specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.