Use of bitset

Source: Internet
Author: User

Some programs need to process binary ordered sets. The Standard Library provides the bitset type. In fact, bitset is a binary container, and each element in the container is a binary code or 0, or 1. In addition to accessing the bit bits of the specified lower mark, bitset can also take them as an integer for some statistics.

1. Definition and initialization

Usage:

# Include <bitset>

Using STD: bitset;

When defining a bitset, you must specify the number of bits in a bitset. The number of BITs is an integer constant:

Bitset <n> B; // B has n digits, each of which is 0.

Bitset <n> B (U); // B is a copy of the unsigned Long U.

Bitset <n> B (s); // B is a copy of the bits contained in String object S. This s must be a bits, that is, a binary string.

Bitset <n> B (S, POs, n); // B is a copy of the N-digit starting from position POs in S.

2. bitset operation

The problem with expressing bitwise Vectors Using the integer type is that it is relatively low and complicated to set reset and test individual bitwise hierarchies using the bitwise operator.

For example, if the integer type is used to set the 27th bits to 1, we write

Quiz1 | = 1 <27;

We can use bitset to write

Quizl [27] = 1;

Or

Quiz1.set (27 );

To use the bitset class, we must include the relevant header file.

# Include <bitset>

Bitset has three declaration methods. In the default definition, we only need to simply specify the length of the bit vector, for example

Bitset <32> bitvec; declares the order of 32 bit bitset object bits from 0 to 31. By default, all bits are

The value is initialized to 0. to test whether the bitset object contains a bit set to 1, we can use any () to operate when bitset

If one or more of the objects are set to 1, any () returns true for bitvec. The following test

Bool is_set = bitvec. Any ();

The result is false, of course. On the contrary, if all bits of the bitset object are set to 0, the none () operation returns

True for bitvec Testing

Bool is_not_set = bitvec. None ();

Returns the number of BITs set to 1.

Int bits_set = bitvec. Count ();

We can use the Set () operation or subscript operator to set a single bit. For example, the for loop below sets the even bit to 1.

For (INT Index = 0; index <32; ++ index)

If (index % 2 = 0)

Bitvec [Index] = 1; similarly, there are two ways to test whether a single bit is 1. The test () operation uses location as a parameter and returns true.

Or false, for example

If (bitvec. Test (0 ))

// Our bitve [0] can work!

We can also use the subscript operator.

Cout <"bitvec: Positions turned on: \ n \ t ";

For (INT Index = 0; index <32; ++ index)

If (bitvec [Index])

Cout <index <"";

Cout <Endl;

To set a single bit to 0, we can use the reset () or subscript operator to set the first bitvec to 0 in the following two operations.

// Both are equivalent. Set the first value to 0.

Bitvec. Reset (0 );

Bitvec [0] = 0;

We can also use the Set () and reset () operations to set all bits of the entire bitset object to 1 or 0, you only need to call the corresponding operation without passing the location parameter. for example

// Set all bits to 0

Bitvec. Reset ();

If (bitvec. None ()! = True)

// Oh! Wrong

// Set all bits to 1if (bitvec. Any ()! = True)

// Oh! Again wrong

Flip () operation to flip the entire bitset object or an independent bit

Bitvec. Flip (0); // flip first

Bitvec [0]. Flip (); // It is also the first place to flip

Bitvec. Flip (); // flip the values of all BITs

There are two other methods to construct bitset objects. They both provide a method to initialize a bitset object to 1: one way is to provide the constructor with a digital display of the First N bits of the bitset object of an unsigned parameter as the corresponding bits of the parameter, for example

Bitset <32> bitvec2 (0 xFFFF );

Set bitvec2's 16-bit low to 1

The following definition of bitvec3

Bitset <32> bitvec3 (012 );

Set the values of 1st and 3 to 1. Assume that the positions are 0-open.

We can also pass a string parameter representing a set of 0 and 1 to construct a bitset object as follows:

// It is equivalent to bitvec3 Initialization

String bitval ("1010 ");

Bitset <32> bitvec4 (bitval );

The 1st and 3 bits of bitvec4 and bitvec3 are both set to 1 while the other bits are kept to 0.

Bitvec. Set ();

3. Related functions

B. Any () // is there a binary bit set to 1 in B?

B. None () // the same effect as B. Any ()

B. Count () // does B have a binary digit set to 1?

B. Size () // Number of binary bits set to 1 in B

B [POS] // access B's binary position at the POs

B. Test (POS) // The binary position at the POs in B is 1

B. Set () // set all binary bits in B to 1

B. Set (POS) // set the binary position of B at the POs to 1.

B. Reset () // set all binary bits in B to 0

B. Reset (POS) // set the binary position of B at the POs to 0.

B. Flip () // reverse all binary values in B by bit

B. Flip (POS) // reverse the binary position at the POs in B

B. to_ulong () // return an unsigned long value with the same binary bits in B

OS <B // outputs the bit set in B to the OS stream


Experiment code and running result:

#include "stdafx.h"#include <iostream>#include <bitset>using namespace std;int _tmain(int argc, _TCHAR* argv[]){//bitset的所有操作://any();none();test();bit[];set();reset();to_string();to_ulong();//count();flip();分别用十进制,八进制,十六进制,字符串赋值。bitset<32> bitvec(8);//0~31,将数字8转为2进制形式存储。0001000000....bool flag = bitvec.any();//判断是否存在某位或者多位为1,有则返回truebool flag1 = bitvec.none();//判断是否所有的位都是0,是则返回truebool flag2 = bitvec.test(3);//测试第4位是否为1,是则返回true<<cout<<"bitvec的值为:"<<bitvec<<endl;cout<<"第4位为:"<<bitvec[3]<<endl;//输出第4位的值bitvec.reset(3);//将第4位设置为0,或者bitvec[3] = 0cout<<"bitvec.reset(3)之后第4位为:"<<bitvec[3]<<endl;//输出第4位的值bitvec.reset();//将所有位设置为0cout<<"bitvec.reset()之后bitvec的值为:"<<bitvec.to_string()<<endl;bitvec.set();//将所有位设置为1cout<<"bitvec.set()之后bitvec.to_string()的值为:"<<bitvec.to_string()<<endl;cout<<"bitvec的值为:"<<bitvec<<endl;cout<<"bitvec.to_ulong()的值为:"<<bitvec.to_ulong()<<endl;cout<<"bitvec中1的个数为:"<<bitvec.count()<<endl;bitvec = 8;cout<<"bitvec = 8之后bitvec的值为:"<<bitvec.to_string()<<endl;bitvec.flip();//将所有的位翻转cout<<"bitvec.flip()之后bitvec的值为:"<<bitvec.to_string()<<endl;bitvec.flip(0);//翻转第一位cout<<"bitvec.flip(0)之后bitvec的值为:"<<bitvec.to_string()<<endl;bitvec = 0xffff;//设置低16位为1cout<<"bitvec = 0xffff之后bitvec的值为:"<<bitvec.to_string()<<endl;bitvec = 012;//用八进制值012设置bitvec,数字前加0表示为8进制!!!cout<<"bitvec = 012之后bitvec的值为:"<<bitvec.to_string()<<endl;string bit = "1011";bitset<32> bitvec1(bit);//用字符串对象初始化bitset<32>对象cout<<"bitvec1的值为:"<<bitvec1.to_string()<<endl;string bit1 = "1111110101100011010101";bitset<32> bitvec2(bit1,6);//用从第6位开始到字符串结束这一部分初始化bitvec2cout<<"bitvec2的值为:"<<bitvec2.to_string()<<endl;bitset<32> bitvec3(bit1,6,4);//用从第6位开始,长度为4这一部分初始化bitvec3;cout<<"bitvec3的值为:"<<bitvec3.to_string()<<endl;getchar();return 0;}


Use of bitset

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.