In computer hardware and software, bit is the smallest unit of data, and as programmers we use them in every single character of our code lines and more. A std::bitset
is a modern feature to use set of bits in C++. C++ is great programming language to use every part of hardware of a device and you can use C++ program easily with a professional compiler and by using this kind of examples below.
First of all, let’s remind ourselves of what a bit and a byte is.
Table of Contents
What is a bit, what is byte?
In computer hardware and software, bit is the smallest unit of data, a binary digit that shows it is on or off by using 1 and 0 numbers. The word ‘bits’ refers to transistors in the memory hardware which can be manipulated by changing its electrical value from on to off (positive to negative, charged to uncharged) which is used to represent 1 to 0 or 0 to 1.
In programming, while we mostly use decimal numbers, computer hardware uses binary numbers and operates in bits. Every alpha-numeric ASCII character from 0 to 255 is formed by 8 bits in binary mode that is called 1 byte. This concept allows us to store everything in bytes, in other words in 8 bits of blocks, from single bit to large data blocks like tables, pictures, sounds, movies, static and dynamic libraries.
What is std::bitset?
A bitset (std::bitset
) is an array of bits, array of boolean numbers, defined in header <bitset>
. In comparison, space taken by bitset is less than using a bool or a vector of bits. bitsets are sets of bits that mean they have a limitation of bits known at compile time.
In C++ Syntax, it is the class template that represents a fixed-size sequence of N
bits. Here is the Syntax,
1 2 3 |
template< std::size_t num_of_bits > class bitset; |
Here is an example of bitset usage that consists of 8 bits,
1 2 3 |
std::bitset<8> bs; |
you can use other set numbers too,
1 2 3 4 5 6 |
std::bitset<8> b8; std::bitset<16> b16; std::bitset<32> b32; std::bitset<64> b64; |
How To Use std::bitset In C++
If you want to use std::bitset
, you should use the #include <bitset>
header in your program, like we show below.
1 2 3 4 5 6 7 8 |
#include <bitset> int main() { std::bitset<8> bs; } |
You can set each bit like so:
1 2 3 4 5 6 7 8 9 10 11 12 |
std::bitset<8> bs; bs[0] = 1; bs[1] = 0; bs[2] = 0; bs[3] = 1; bs[4] = 0; bs[5] = 1; bs[6] = 1; bs[7] = 1; |
Or you can set bits in a string form as below.
1 2 3 |
std::bitset<8> bset( std::string("11001100") ); |
Here you need string header to use std::string
. You can access to each bits of a bitset like so:
1 2 3 4 5 6 7 8 9 10 11 12 |
std::bitset<8> bs; bool a = bs[0]; bool b = bs[1]; bool c = bs[2]; bool d = bs[3]; bool e = bs[4]; bool f = bs[5]; bool g = bs[6]; bool h = bs[7]; |
You can simply print out bs in binary mode.
1 2 3 |
std::cout << bs << std::endl; |
You can get size of a bitset like we show here:
1 2 3 |
int size_of_bitset = bs.size(); |
You can count number of bits which are set to 1 as below,
1 2 3 |
int number_of_ONs = bs.count(); |
You can also count number of bits which are set to 0 as below,
1 2 3 |
int num_of_OFFs = bs.size()-bs.count(); |
How to use bitset in C++ Builder
If you want to use std::bitset
in your C++ Builder apps, just #include <bitset>
header.
1 2 3 |
#include <bitset> |
Is there a simple VCL example to how to use bitset in C++ Builder?
Here is a simple C++ Builder VCL example to use std::bitset
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <vcl.h> #include <bitset> #include <string> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { std::bitset<8> bset( std::string("11001100") ); } |
Is there a simple FMX example to how to use bitset in C++ Builder?
Here is a simple C++ Builder FMX example to use std::bitset
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <fmx.h> #include <bitset> #include <string> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.fmx" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { std::bitset<8> bset( std::string("11001100") ); } |
How to use bitset with the Dinkumware Standard C++ Library?
Note that, the Dinkumware Standard C++ Library is the implementation of the C++ Standard Library that C++Builder uses for applications on the 32-bit Windows, 64-bit Windows and macOS target platforms. The libraries include containers such as vectors, lists, sets, maps, and bitsets. Dinkumware also includes algorithms for common operations, such as sorting a container or searching inside a container.
Dinkumware version 8.03a is available specifically for C++ application development on target platforms that support the following Clang-enhanced compilers: BCC32C, BCC32X and BCC64
How to use bitset in professional C++ programming
In professional programming bitsets are great to tool to handle any data blocks in it forms. This allows you access and manipulate data in a bit form easily. Decoding, encoding, zipping and unzipping data operations can be applied easily. You can use bitsets to store a lot of ON/Off options in your database or in save files of your applications, in your games etc. For example, you can hold 32 different permissions of a user just in std:bitset<32> bs;
. Thus, you will only store 4 bytes in one variable instead of 32 tiny integers for each permission options. Bits are faster in operations, you can use bitsets to manipulate different color bits (8, 16, 24, 32 bits). You can easily set down reds, blues, or you can manipulate alpha channel bits of colors.
Is there a full example to use std::bitset in C++?
Here is the full example that includes all examples of how to use std::bitset
in C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
#include <iostream> #include <bitset> #include <string> int main() { std::bitset<8> bs; bs[0] = 1; bs[1] = 0; bs[2] = 0; bs[3] = 1; bs[4] = 0; bs[5] = 1; bs[6] = 1; bs[7] = 1; bool a = bs[0]; bool b = bs[1]; bool c = bs[2]; bool d = bs[3]; bool e = bs[4]; bool f = bs[5]; bool g = bs[6]; bool h = bs[7]; std::bitset<8> b8; std::bitset<16> b16; std::bitset<32> b32; std::bitset<64> b64; std::bitset<8> bset( std::string("11001100")); int size_of_bitset = bs.size(); int number_of_ONs = bs.count(); int num_of_OFFs = bs.size()-bs.count(); std::cout << bs << std::endl; system("pause"); return 0; } |
You can find more examples about bits and bit operations in learncplusplus.org web site, just search for the keywords.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.