Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a builtin bitset that's similar to the std::bitset from C++?

Tags:

python

bitset

I want to use a bit array in Python that I could use like the standard bitset from C++. Example:

#include<bitset>
int main() {
    std::bitset<100> numBits;
}

However, I don't know if there is something similar in Python, preferably built-in.

like image 998
starkshang Avatar asked Dec 28 '15 15:12

starkshang


People also ask

What is bitset in C?

Bitset represents a fixed-size sequence of N bits and stores values either 0 or 1. Zero means value is false or bit is unset and one means value is true or bit is set. Bitset class emulates space efficient array of boolean values, where each element occupies only one bit.

What is bitset <> in C++?

Bitset is a container in C++ Standard Template Library for dealing with data at the bit level. 1. A bitset stores bits (elements with only two possible values: 0 or 1). We can however get the part of a string by providing positions to bitset constructor (Positions are with respect to string position from left to right)

Is there a bitset in Python?

A Python interface to the fast bitsets in Sage. Bitsets are fast binary sets that store elements by toggling bits in an array of numbers. A bitset can store values between and capacity - 1 , inclusive (where capacity is finite, but arbitrary).

Does STL have bitset?

Second, bitset is not a Sequence; in fact, it is not an STL Container at all.


1 Answers

Normally, you just the builtin int class (or long in python2). Maybe wrap it in a class if you particularly care about hiding the shifts.

like image 94
o11c Avatar answered Oct 13 '22 08:10

o11c