The value of each bit of the variable is taken out by C + + (first known and Qbitarray)

Source: Internet
Author: User
Tags bitset

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.

1 uint j = 1;
2 for(uint k = 0;k < sizeof(uint) * 8;(j = j<<1),k++){
3     std::cout << "The "<< k << " bit is :"<< (i&j)  << std::endl;
4 }

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>
02 #include <QVariant>
03 #include <QBitArray>
04 #include <QDebug>
05 #include <bitset>
06 #include <iostream>
07
08 intmain(int argc, char*argv[])
09 {
10     QCoreApplication a(argc, argv);
11     uint i = 200;
12
13     QBitArray bits;
14     bits.resize((sizeof(uint) * 8));
15
16     uint j = 1;
17     for(uint k = 0;k < sizeof(uint) * 8;(j = j<<1),k++){
18         bits.setBit(k,(i&j));
19     }
20
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);
29
30     returna.exec();
31 }

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.