The pre-write program is only a byte-level operation, with Char and memcpy for a series of memory operations. In this one SDK, the status value is directly one indicator, so we need to take out each bit to operate. Of course, CPP also has a wealth of bit operations, but although also learned, know the meaning, but the actual is almost useless. This is the only time we can use it.
The first method: The idea is to use all the bits with, so that you can take out whether each bit is 1. Directly on the code bar, anyway to see also understand.
2 |
for (uint k = 0;k < sizeof (uint) * 8;(j = j<<1),k++){ |
3 |
std::cout << "The " << k << " bit is :" << (i&j) << std::endl; |
This is an unsigned integer, of course other types ,
The second way, is to use stl,cpp processing convenience is indeed more convenient. There are great bitset.
Its constructor can directly help you to remove the value into the Bitset, which supports the Ullong type as well as the string type.
It can also be converted directly to a string type. The disadvantage is that its size must be specified in the compiler.
Just note: The position taken out of the bitsit and the converted string are not the same (the personal test concludes that Bitset is right-to-left and string is from sitting to the right).
There is also a class for processing bits in QT: Qbitarray
This class can be changed in size, with resize () to set the new size. Just this class can only be a bit of yourself, a bit of assignment (Qbitarray is left-to-right direction).
Qbitarray and Bitset both overloaded &,~,|. , ^ these bit arithmetic symbols.
Here's an example: (Note bitset[n] and String[n] take the same direction. )
01 |
#include <QCoreApplication> |
08 |
int main( int argc, char *argv[]) |
10 |
QCoreApplication a(argc, argv); |
14 |
bits.resize(( sizeof (uint) * 8)); |
17 |
for (uint k = 0;k < sizeof (uint) * 8;(j = j<<1),k++){ |
21 |
qDebug() << "QBitArray bits :" << bits; |
22 |
std::bitset< sizeof (uint) * 8> bit(i); |
23 |
std::cout << "std::bitset bit : " << bit << std::endl; |
24 |
std::string str = bit.to_string(); |
25 |
std::cout << "std::bitset bit.to_string : " << str << std::endl; |
26 |
std::cout << "std::bitset bit at 3 : " << bit[3] << std::endl; |
27 |
std::cout << "std::string str at 3 : " << str.at(3) << std::endl; |
28 |
qDebug() << "QBitArray bits at 3 :" << bits.at(3); |
The output is:
Http://www.dushibaiyu.com/2015/07/cpp_get_bit_value.html
The value of each bit of the variable is taken out by C + + (first known and Qbitarray)