In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about the pits of the bitfield data type, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
Disclaimer: this is from a long time ago.
Bitfield is not portable, so in practice, I try to use bitand instead.
However, the definition of bitfield has been used in the code before, as a follow-up development, I have no reason to change this data structure (unless there is something wrong with it). As a result, I accidentally stepped into this pit.
The definition and use of bitfield are roughly as follows:
Union utest {
Int val
Struct stest {
Int a:3
Int b:5
}
}
Union utest t
T.value = 0x07
The number after the bitfield colon identifies the bit width of the bitfield, and the type in front of the bitfield is used to identify what type should be changed after the field is taken out (the standard only supports int, signed int, unsigned int, while gcc also supports char, short, etc.).
The crux of the problem is that if you define a signed type, the compiler will upgrade the extracted bitfield according to the signed type.
When the program reads the variable stest::a, it reads the low 3bit of utest::val 's byte0. Because the type of stest::an is int (there is a symbolic type), he uses utest::byte0::bit2 as a symbol bit for integer promotion.
That is, if the value of utest::byte0::bit2~0 is 110b, then when you read stest::a, the compiler will use bit2 as a symbol bit to promote 110b integer to 0xfffffffe, that is, (int)-2
In practice, I use stest::b as the index of an array, and when stest::b is greater than 0x10, array access is directly out of bounds.
Btw, generally speaking, when using the bitfield feature, should rarely rely on its symbolic extension function (that is, defining it as a symbolic type), so you must think twice when defining bitfield as int rather than unsigned int.
After reading the above, do you have any further understanding of the bitfield data type pit? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.