Bitset usage Finishing

Source: Internet
Author: User
Tags bit set bitset

In the project you need to use the number of 10 in 48 digits in binary from high to low to explain, and then per 0 or one to determine the alarm or error status.

Therefore, in Linux in C + + requires the use of binary conversion and bitwise parsing. Collect some information, save your own AH.

As follows:

Bitset Usage Finishing
Constructors
Bitset<n> b;
B has n bits, each digit is 0. parameter n can be an expression.
If bitset<5> B0, then "B0" is "00000";

Bitset<n> B (unsigned long u);
B has n bits and is assigned with u; if u is more than n bit, the top is truncated
For example: Bitset<5>b0 (5); then "B0" is "00101";

Bitset<n> B (string s);
B is a copy of the bit string contained in string object s
String Bitval ("10011");
Bitset<5> B0 (BITVAL4);
Then "B0" is "10011";


Bitset<n> B (S, POS);
B is a copy of the starting position of POS in S, and the previous redundant bits are automatically populated 0;
String Bitval ("01011010");
Bitset<10> B0 (BITVAL5, 3);
Then "B0" is "0000011010";

Bitset<n> B (S, POS, num);
B is a copy of Num Single-digit starting from position Pos in S, if num<n, the front vacancy is automatically filled 0;
String Bitval ("11110011011");
Bitset<6> B0 (BITVAL5, 3, 6);
Then "B0" is "100110";





OS << b
Output the bit set in B to the OS stream
OS >>b
Enter into B, such as "cin>>b", and if you enter a character other than 0 or 1, only the bits in front of the character is taken.

BOOL Any ()
Whether there is a bits that is placed to 1. As opposed to none ()

bool None ()
Does not exist a bits that is set to 1, that is, all 0. The opposite of any ().

size_t count ()
The number of BITS is 1.

size_t size ()
Number of Bits

Flip ()
Put all the bits in the opposite position.

Flip (size_t POS)
To reverse the bits at the POS

BOOL operator[] (Size_type _pos)
Get the bits at the POS

Set ()
Put all the bits into 1.

Set (POS)
Place the binary position at the POS at 1

Reset ()
Put all the bits into 0.

Reset (POS)
Place the binary position at the POS at 0

Test (size_t POS)
Whether the bits at the POS is 1.

unsigned long To_ulong ()
Returns a unsigned long value with the same bits

String to_string ()
Returns the corresponding string.


Please read MSDN for details.

Another turn: http://blog.csdn.net/auto_ptr/archive/2010/10/22/5958924.aspx


examples of the use of Bitset

Std::bitset is a template class of STL, its parameter is the numerical value of the shaping, the way of using bit and the difference of the group is little, the equivalent can only save a bit of array. Let's look at an example

View Plaincopy to Clipboardprint?
Bitset<20> B1 (5);
cout<< "The set bits in Bitset<5> B1 (5) is:"
<< B1 <<endl;
Bitset<20> B1 (5);
cout<< "The set bits in Bitset<5> B1 (5) is:"
<< B1 <<endl;

The result is the set bits in Bitset<5> B1 (5) is:00000000000000000101

It is passed in As Integer 5, and is printed in binary numbers.

Bitset can also be used as a string to an integral type

View Plaincopy to Clipboardprint?
String Bitval2;
cin>>bitval2;
int length = strlen ("11101101100");
BITSET<11>B2 (BITVAL2);
Bitset<11> B2 (Bitval2);
cout<<b2<< "is" <<b2.to_ulong () <<endl;
String Bitval2;
cin>>bitval2;
int length = strlen ("11101101100");
BITSET<11>B2 (BITVAL2);
Bitset<11> B2 (Bitval2);
cout<<b2<< "is" <<b2.to_ulong () <<endl;

and shaping into a string

View Plaincopy to Clipboardprint?
int interge1;
cin>>interge1;

cout<< "************ Integer to String **************" <<endl;
BITSET<11>B3 (INTERGE1);
Cout<<b3.to_ulong () << "is" <<b3.to_string () <<endl;
int interge1;
cin>>interge1;

cout<< "************ Integer to String **************" <<endl;
BITSET<11>B3 (INTERGE1);
Cout<<b3.to_ulong () << "is" <<b3.to_string () <<endl;

On the internet to see there is a good article about Bitset wrote, do not know who the author is, paste for their own use:

Bitset How to initialize, how to convert to a double decimal, how to cross (you can try to use string as an intermediate, because Bitset can be initialized with string, but such constructs and passes can take a lot of time-I hate this unnecessary consumption.) )

If I want the precision of the calculation to be high enough to take the Bitset to 64 bits, then what type of number can be output. If no output is required, how to convert a 64-bit bitset to a double decimal in the precision. (You may need to implement it yourself)

How to convert a double type number to Bitset, or binary code, to make it easier for us to cross and mutate.

(To put it simply, the above two is the problem of decoding and coding)--the text is messy and tidy.

How to implement the merging of two bitset. The decimal part, the integral part, if can merge, that writes the procedure to be more convenient. For example, two 32-bit Bitset are merged into a 64-bit bitset. (Do you want to use string for conversion?) How to convert. )

Code Description: Place the bitset in a position of 1

Bitset<32> bits;
for (int i =0;i<5;i++)
Bits. Set (i); I is the number of digits to be placed to 1
cout<<bits<<endl;
Functional usage of Bitset


Attention matters

You can see why the following code outputs 7 and 9.


#include <iostream>
#include <bitset>
using namespace Std;
void Main ()
{
Bitset<4> bit (1111);
Cout<<bit.to_ulong () <<endl;
Bitset<4> AIT (1001);
Cout<<ait.to_ulong () <<endl;
}

The reason is simple: Bitset called the constructor, 1111 for the decimal, replaced by the binary system for the 0x10001010111, the last 4 digits 0111, the output is 7; If you want to specify each of the bitset inside, then it is best to use String type: bitset<4 > Bits ("1111"); So the output is 15.

String merging and output problems, to be done, is really troublesome ... To be lazy, to wander between multiple types ... But it's really easy to write, haha. There is a ready-made way to use Bai. No matter how efficient ...

#include <iostream>
#include <string>
#include <iostream>
#include "AFXWIN.h"
using namespace Std;

int main () {
CString S1 = "ABCD";
CString s2 = "XYZW";

CString s3 = S1+s2;
cout<< (LPCTSTR) s1<<endl<< (LPCTSTR) s3<<endl;
String S4 = (LPCTSTR) S3;
cout<<s4<<endl;
return 0;
}

Here are 2 code examples of Bitset merging

#include <bitset>
#include <iostream>
#include <string>
#include <iostream>
#include "AFXWIN.h"
using namespace Std;

int main () {
Bitset<4> bits1 ("1111");
bitset<4> bits2 ("0000");
int i = Bits1.size () +bits2.size ();
Bitset<i> Bits3; Can not use dynamic parameters as template parameters, can find a way to solve.
Bitset<128> Bits3;
int j=0;
For (J=0;j<bits1.size (); j + +)
{
if (bits1[j]==1)
Bits3. Set (j);
}
For (J=bits1.size (); J<bits1.size () +bits2.size (); j + +)
{
if (Bits2[j-bits1.size ()]==1)
{
Bits3. Set (j);
}
}
Cout<<bits3<<endl<<bits3.to_ulong () <<endl;
return 0;
}

Maximum length that bitset can reach

#include <bitset>
#include <vector>
#include <iostream>
#include <string>
#include <iostream>
#include "AFXWIN.h"
using namespace Std;

int main () {
Bitset<1000000> bits;//1 million is almost up to the top, and if you add another 0 and reach 10 million, it crashes. Why.
cout<<bits[0];
return 0;
}

Want to use a dynamic bitset?

Dynamic_bitset can meet my needs. This is really great. Hooray for boost. PS: I do not know how much efficiency impact it will cause. and fixed-length code, although fixed a bit, waste a little space, but if faster, it is worth it. In addition: Dynamic_bitset can not be compiled under VC6 ...

Bit_vector

This "bit vector group" is implemented in the SGI STL, not in VC6. It can be seen from the introduction of names and functions: This is a container that can be as convenient as the operation of a vector, and can push_back every bit. The efficiency remains to be tested, and I stumbled across the library in a book.

However, I am disappointed: in Ubuntu and VC6, there is no bit_vector, you must install the SGI version of the STL to do it.


Conclusion: In this respect, it seems to make up for it. (To achieve a simple bitset, space, just a little waste of it).

With regard to the 7,9 mentioned in the previous example, some questions were raised as follows:

Compilation results of 7 and 9, please explain the master do not understand

#include <iostream>

#include <bitset>

using namespace Std;

void Main () {

Bitset<4> bit (1111);

Cout<<bit.to_ulong () <<endl;

Bitset<4> AIT (1001);

Cout<<ait.to_ulong () <<endl;

}

Question added: What about the code for the following? How do I explain the result of 9 #include <iostream> #include <bitset>using namespace std;void main () {bitset< 5> A (00111); Cout<<a.to_ulong ();}

The answer is as follows :

The MSDN says:

Bitset (unsigned long Val);

The constructor sets only those bits in position J for which Val & 1 << J are nonzero.

After understanding this passage, calculate it with a calculator:

D (1111) = B (10001010111)

D (1001) = B (1111101001)

D is decimal, B is binary and you define the BITSET<4> only four digits, then the last four digits of the binary of 1111 are 0111, the decimal 7, and the same 1001 gets 1001, that is, decimal 9

PS: For you to add, I would say if it is bitset<5> (111), then this 111 is decimal, but when you become bitset<5> (00111) or bitset<5> (0111), So this 111 is the octal, that is, the decimal 73, that is, 1001001, take the back five digits that is 9 to say a little bit. N in,bitset<n> (M) indicates the number of digits in memory, binary.

m indicates the size of the number, as for the number of the system, you can check the data, C language in the front of an integer plus 0 for octal, before an integer plus 0x represents 16. References: MSDN My Code:

#include <iostream> #include <bitset> using namespace std;
Bitset All operations://any (); none (), test (); Bit[];set (); reset (); to_string (); To_ulong ();
Count (), Flip (), and assign values in decimal, octal, hexadecimal, and string, respectively.  int main () {bitset<32> Bitvec (8);//0~31 BOOL flag = Bitvec.any ();//Determine whether there is a bit or more than 1, then return true bool Flag1 = Bitvec.none ()//To determine whether all bits are 0, is to return true bool Flag2 = Bitvec.test (3);//test 4th bit is 1, return true<< cout<< "Bitvec
    The value is: "<<bitvec<<endl; cout<< "4th bit is:" <<bitvec[3]<<endl;//output 4th digit value Bitvec.reset (3);//Set 4th bit to 0, or bitvec[3] = 0 cout<< "The 4th bit is:" <<bitvec[3]<<endl;//output 4th bit value Bitvec.reset ()//set all bits to 0 cout<< "Bitvec value is:" <<
    Bitvec.to_string () <<endl;
    Bitvec.set ()//Set all bits to 1 cout<< "Bitvec value is:" <<bitvec.to_string () <<endl;
    cout<< "Bitvec value is:" <<bitvec<<endl;
    cout<< "Bitvec value is:" <<bitvec.to_ulong () <<endl; The number of 1 in the cout<< "Bitvec" is: "<<biTvec.count () <<endl;
    Bitvec = 8;
    cout<< "Bitvec value is:" <<bitvec.to_string () <<endl;
    Bitvec.flip ();//Flip All bits cout<< "Bitvec value is:" <<bitvec.to_string () <<endl;
    Bitvec.flip (0)//Flip the first bit cout<< "Bitvec value is:" <<bitvec.to_string () <<endl;
    Bitvec = 0xffff;//Set low 16 bit to 1 cout<< "Bitvec value is:" <<bitvec.to_string () <<endl;
    Bitvec = 012;//octal value 012 set Bitvec cout<< "Bitvec" <<bitvec.to_string () <<endl;
    String bit = "1011"; Bitset<32> bitvec1 (bit); Initialize Bitset<32> object with string Object cout<< "BITVEC1 value is:" <<bitvec1.to_string ()
    <<endl;
    String bit1 = "1111110101100011010101"; Bitset<32> bitvec2 (bit1,6); initializes the bitvec2 cout<< "bitvec2 with the value" <<bitvec2.to_ from 6th to the end of the string: "
    String () <<endl;
    Bitset<32> BITVEC3 (bit1,6,4); The BITVEC3 is initialized with a length of 4 starting from the 6th digit;
cout<< "BITVEC3 value is:" <<bitvec3.to_string () <<endl;
 }


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.