C++ boost::dynamic_bitset Class with Examples Last Updated : 12 Jul, 2025 Comments Improve Suggest changes Like Article Like Report The boost has more than 150 libraries in it, where a couple of most frequently used libraries were already included in C++ standard library. The dynamic_bitset is a powerful library used for bit manipulation. The dynamic_bitset class is used to represent a set of bits in either 0(reset) or 1(set) form. dynamic_bitset is an improvement over bitset (std::bitset and boost::bitset) which allocates any required length of bits at runtime, contrary to bitset whose bit's length has to be determined at compile time.The dynamic_bitset if found under the boost header boost/dynamic_bitset.hpp. Syntax: boost::dynamic_bitset <uint8_t> B (N, num); The parameters of the constructors are N which signifies the required number of bits in the set.num signifies any integral value whose bits will be stored.uint8_t signifies the block size (8 here, it can also be empty if we don't need to specify block size). Each individual bits of the dynamic_bitset can be accessed similar to bitset indexing operator []. Please note that bit representation of a number B in dynamic_bitset of length n is represented as: B[n-1] B[n-2] ... B[1] B[0] In other words, the indexing in dynamic_bitset works in reverse order (similar to bitset). Each bit in dynamic_bitset takes exactly 1-bit space because of its optimization thus making operations faster than boolean vector.Member Functions: The basic member functions that can be performed on dynamic_bitset are listed below: set(): It assigns every bit with 1reset(): It assigns nth bit with 0 if the n is passed in argument, else it clears the entire bitset object.flip(): It inverts any given bit i.e. toggle 0 to 1 or vice-versasize(): It returns the size of the dynamic_bitset objectresize(): It is used to increase or decrease the size of the objectpush_back(): It increases the size of dynamic_bitset object by one and pushes the value at MSBpop_back(): It removes one bit from MSBnum_blocks(): It returns the number of blocks in bitsetappend: It appends the bits at the most-significant bit. This increases the size of the bitset by bits_per_block. For i in the range [0, bits_per_block), the bit at position (size + i) is set to ((value >> i) & 1)empty(): It returns true if the length of dynamic_bitset is 0, else it returns false.count(): It returns the number of set bits in dynamic_bitset.all(): It tests if all of the bits in dynamic_bitset are set, if they are it returns true else false.any(): It tests if atleast one of the bit in dynamic_bitset is set, if they are it returns true else false.none(): It tests if none of the bits in dynamic_bitset are set, if none of them are set then it returns true else false.test(): It tests if the ith bit is set or not. If set it returns true else false. Example 1: CPP #include <boost/dynamic_bitset.hpp> #include <iostream> using namespace std; int main(int argc, char* argv[]) { int bit_size = 8; // B1 is initialized with size 0 // with all bits 0 boost::dynamic_bitset<> B1; // B2 is initialized with size // bit_size with all bits 0 boost::dynamic_bitset<> B2(bit_size); // B3 is initialized with size // bit_size and value 14 boost::dynamic_bitset<> B3(bit_size, 14); // B4 is initialized with size // bit_size, value 14 and // block_size of 8 bits boost::dynamic_bitset<uint8_t> B4(16, 84); // Empty cout << "Content of B1 is: " << B1 << endl; // 00000000 cout << "Content of B2 is: " << B2 << endl; cout << "Binary representation of 14 in 8 bit: " << B3 << endl; cout << "Content of B4 is: " << B4 << endl << endl; // Setting 1 st of B2 to 1 cout << "Content of B2 before set(): " << B2 << endl; B2.set(0); cout << "Content of B2 after set(0): " << B2 << endl; // Setting every bits of B22 to 1 B2.set(); cout << "Content of B2 after set(): " << B2 << endl; // Resetting 2nd bit to 0 B2.reset(1); cout << "After resetting 2nd bit of B2: " << B2<< endl; // Resetting every bit to 0 B2.reset(); cout << "After resetting every bit of B2: " << B2 << endl; // Flipping first bit of B3 cout << "Content of B3 before flip(): " << B3 << endl; B3.flip(0); cout << "Content of B3 after flip(0): " << B3 << endl; // Flipping every bits of B3 B3.flip(); cout << "Content of B3 after flip(): " << B3 << endl << endl; // Size of B1, B2, B3, B4 cout << "Size of B1 is: " << B1.size() << endl; cout << "Size of B2 is: " << B2.size() << endl; cout << "Size of B3 is: " << B3.size() << endl; cout << "Size of B4 is: " << B4.size() << endl << endl; // Resizing B1 to size 4, // default bit-value 0 B1.resize(4); cout << "B1 after increasing size to 4 bits: " << B1 << endl; // Resizing B1 to size 8, bit-value 1. // If num_bits > size() then the bits // in the range [0, size()) remain the same, // and the bits in [size(), num_bits) // are all set to value (here 1). B1.resize(8, 1); cout << "B1 after increasing size to 8 bits: " << B1 << endl; // Resizing B1 to size 1 i.e. // slicing [1, B1.size()-1) B1.resize(1); cout << "B1 after decreasing size to 1 bit: " << B1 << endl << endl; // Pushing a set bit at MSB in B1 B1.push_back(1); cout << "B1 after push(1) operation: " << B1 << endl; // Pushing a reset bit at MSB in B1 B1.push_back(0); cout << "B1 after push(0) operation : " << B1 << endl << endl; // Popping 1 bit from MSB in B1 cout << "B1 before pop operation: " << B1 << endl; B1.pop_back(); cout << "B1 after pop operation: " << B1 << endl << endl; // Number of blocks = number of bits / block size cout << "Number of blocks in B4: " << B4.num_blocks() << endl << endl; // Checking if any bitset is empty cout << "B1 is " << (B1.empty() ? "empty" : "not empty") << endl; cout << "B2 is " << (B2.empty() ? "empty" : "not empty") << endl; // Resizing B3 to 0 B3.resize(0); cout << "B3 is " << (B3.empty() ? "empty" : "not empty") << endl << endl; // Counting number of set bits in B4 cout << "Content of B4 is: " << B4 << endl; cout << "Number of set bits in it are: " << B4.count() << endl << endl; // Checking if all of the bits of B2 is set B2.set(); // B2 => 11111111 cout << "All bits in B2 are " << (B2.all() ? "set" : "not set") << endl; B2.reset(2); // B2 => 11111011 cout << "All bits in B2 are " << (B2.all() ? "set" : "not set") << endl << endl; // Checking if any of the bits of B2 is set cout << (B2.any() ? "Atleast one" : "No") << " bit in B2 is set " << endl; B2.reset(); // B2 => 00000000 cout << (B2.any() ? "Atleast one" : "No") << " bit in B2 is set " << endl << endl; // Checking if none of the bits of B2 are set // B2 => 00000000 if (B2.none()) cout << "None of the bits in B2 is set"; else cout << "Atleast one bit in B2 is set"; cout << endl << endl; // Testing if 1st bit of B1 is set or not cout << "Content of B1 is: " << B1 << endl; if (B1.test(1)) cout << "B1[1] is set"; else cout << "B1[1] is reset"; return 0; } Output: Content of B1 is: Content of B2 is: 00000000 Binary representation of 14 in 8 bit: 00001110 Content of B4 is: 0000000001010100 Content of B2 before set(): 00000000 Content of B2 after set(0): 00000001 Content of B2 after set(): 11111111 After resetting 2nd bit of B2: 11111101 After resetting every bit of B2: 00000000 Content of B3 before flip(): 00001110 Content of B3 after flip(0): 00001111 Content of B3 after flip(): 11110000 Size of B1 is: 0 Size of B2 is: 8 Size of B3 is: 8 Size of B4 is: 16 B1 after increasing size to 4 bits: 0000 B1 after increasing size to 8 bits: 11110000 B1 after decreasing size to 1 bit: 0 B1 after push(1) operation: 10 B1 after push(0) operation : 010 B1 before pop operation: 010 B1 after pop operation: 10 Number of blocks in B4: 2 B1 is not empty B2 is not empty B3 is empty Content of B4 is: 0000000001010100 Number of set bits in it are: 3 All bits in B2 are set All bits in B2 are not set Atleast one bit in B2 is set No bit in B2 is set None of the bits in B2 is set Content of B1 is: 10 B1[1] is set Operators: Some of the operators that can be helpful for bit manipulation: operator[]: It returns a reference of nth bit.operator&=(): It performs bitwise-AND with current bitset object and bitset object passed in argument. Eg. B1.operator&=(B2); bitwise-AND of B1 and B2 i.e. B1 & B2 (B1 and B2 must have same number of bits).operator|=(): It performs bitwise-OR with current bitset object and bitset object passed in argument. Eg. B1.operator|=(B2); bitwise-OR of B1 and B2 i.e. B1 | B2 (B1 and B2 must have same number of bits).operator^=(): It performs bitwise-XOR with current bitset object and bitset object passed in argument. Eg. B1.operator^=(B2); bitwise-XOR of B1 and B2 i.e. B1 ^ B2 (B1 and B2 must have same number of bits).operator-=(): It performs set difference with current bitset object and bitset object passed in argument. Eg. B1.operator-=(B2); set difference of B1 and B2 i.e. B1 - B2 (B1 and B2 must have same number of bits).operator=(): It assigns current bitset object with object passed in parameter.operator==(): It validates if current bitset object is exactly equal to that of passed as parameter.operator!=(): It validates if current bitset object is not equal to that of passed as parameter.operator<(): It returns true if current bitset is lexicographically less than the bitset object passed as parameter, else it returns false.operator>(): It returns true if current bitset is lexicographically greater than the bitset object passed as parameter, else it returns false.operator<=(): It returns true if current bitset is lexicographically less than or equal to the bitset object passed as parameter, else it returns false.operator>=(): It returns true if current bitset is lexicographically greater than or equal to the bitset object passed as parameter, else it returns false.operator~(): It creates a copy of the current bitset with all of its bits flipped.operator<<(): It creates a copy of the current bitset object which is shifted to the left by n bits.operator>>(): It creates a copy of the current bitset object which is shifted to the right by n bits.operator<<=(): It shifts the current bitset object to left by n bits.operator>>=(): It shifts the current bitset object to right by n bits. Example 2: CPP #include <boost/dynamic_bitset.hpp> #include <iostream> using namespace std; int main(int argc, char* argv[]) { int n_bits = 8; boost::dynamic_bitset<> B1(n_bits, 123); boost::dynamic_bitset<> B2(n_bits, 206); boost::dynamic_bitset<> temp(4, 3); cout << "Binary representation of 123: " << B1 << endl; cout << "Binary representation of 206: " << B2 << endl << endl; // Operator[] is used to access an individual index // It is a reference of the nth bit. It can be used for // assignment of a boolean value at nth bit cout << "4th bit of B1 consist: " << B1[3] << endl; B1[3] = 0; cout << "Assigning 0 to 4th bit of B1: " << B1 << endl << endl; // Operator= assigns on current bitset object cout << "temp before assignment: " << temp << endl; temp.operator=(B1); cout << "temp after assignment with B1: " << temp << endl << endl; // Operator&= performs bitwise-AND cout << "B1 consist of: " << B1 << endl; cout << "B2 consist of: " << B2 << endl; temp.operator=(B1); temp.operator&=(B2); cout << "B1 AND B2 is : & " << temp << endl << endl; // Operator|= performs bitwise-OR cout << "B1 consist of: " << B1 << endl; cout << "B2 consist of: " << B2 << endl; temp.operator=(B1); temp.operator|=(B2); cout << "B1 OR B2 is : | " << temp << endl << endl; // Operator^= performs bitwise-XOR cout << "B1 consist of: " << B1 << endl; cout << "B2 consist of: " << B2 << endl; temp.operator=(B1); temp.operator^=(B2); cout << "B1 XOR B2 is : ^ " << temp << endl << endl; // Operator-= performs set difference cout << "B1 consist of: " << B1 << endl; cout << "B2 consist of: " << B2 << endl; temp.operator=(B1); temp.operator-=(B2); cout << "Set differ is: " << temp << endl << endl; // Operator== checks if bitset object is equal to // another one, bit length has to be same for true cout << "dynamic_bitset B1 and B2 are " << (operator==(B1, B2) ? "equal" : "not equal") << endl; boost::dynamic_bitset<> B3(2, 0), B4(3, 0); cout << "Content of B3: " << B3 << endl << "Content of B4: " << B4 << endl << "dynamic_bitset B3 and B4 are " << (operator==(B3, B4) ? "equal" : "not equal") << endl; B3.operator=(B4); cout << "dynamic_bitset B3 and B4 are: " << (operator==(B3, B4) ? "equal" : "not equal") << endl << endl; // Operator!= checks if bitset object is unequal cout << "dynamic_bitset B1 and B2 are "; cout << (operator!=(B1, B2) ? "not equal" : "equal") << endl << endl; // Operator< checks if first bitset object is // lexicographically less than second one cout << "B1 consist of: " << B1 << endl; cout << "B2 consist of: " << B2 << endl; if (operator<(B1, B2)) cout << "B1 is lexicographically less than B2"; else cout << "B2 is lexicographically greater than B2"; cout << endl << endl; // Operator> checks if first bitset object is // lexicographically greater than second one cout << "B1 consist of: " << B1 << endl; boost::dynamic_bitset<> B5(8, 0); cout << "B5 consist of: " << B5 << endl; if (operator>(B1, B5)) cout << "B1 is lexicographically greater than B5"; else cout << "B1 is lexicographically less than B5"; cout << endl << endl; // Operator<= checks if first bitset object is // lexicographically less than or equal to second one cout << "B3 is lexicographically "; if (operator<=(B3, B3)) cout << "less than or equal to B3" << endl << endl; else cout << "greater than B3" << endl << endl; // Operator>= cout << "B5 consist of: " << B5 << endl; cout << "B2 consist of: " << B2 << endl; cout << "B5 is lexicographically "; if (operator>=(B5, B2)) cout << "greater than or equal to B2"; else cout << "less than B2"; cout << endl << endl; // Operator~ creates a copy of flipped bitset object cout << "Value of dynamic_bitset B4 : " << B4 << endl; cout << "Creating flipped copy of B4: "; cout << B4.operator~() << endl << endl; // Operator<< creates a copy of current bitset object // which is shifted to left by n times cout << "Value of dynamic_bitset B2: " << B2 << endl; cout << "Copy of B2 left shift 3 times is : "; cout << B2.operator<<(3) << endl; // Operator>> creates a copy of current bitset object // which is shifted to right by n times value of B2 // is not changed, the copy is displayed cout << "Copy of B2 right shift 1 time is : "; cout << B2.operator>>(1) << endl << endl; // Operator<<= shifts the current bitset object // n times to left cout << "Value of dynamic_bitset B2: " << B2 << endl; cout << "B2 left shift 3 times is : "; cout << B2.operator<<=(2) << endl; // Operator>>= shifts current bitset object // n times value to right cout << "B2 right shift 1 time is : "; cout << B2.operator>>=(3); return 0; } Output: Binary representation of 123: 01111011 Binary representation of 206: 11001110 4th bit of B1 consist: 1 Assigning 0 to 4th bit of B1: 01110011 temp before assignment: 0011 temp after assignment with B1: 01110011 B1 consist of: 01110011 B2 consist of: 11001110 B1 AND B2 is : & 01000010 B1 consist of: 01110011 B2 consist of: 11001110 B1 OR B2 is : | 11111111 B1 consist of: 01110011 B2 consist of: 11001110 B1 XOR B2 is : ^ 10111101 B1 consist of: 01110011 B2 consist of: 11001110 Set differ is: 00110001 dynamic_bitset B1 and B2 are not equal Content of B3: 00 Content of B4: 000 dynamic_bitset B3 and B4 are not equal dynamic_bitset B3 and B4 are: equal dynamic_bitset B1 and B2 are not equal B1 consist of: 01110011 B2 consist of: 11001110 B1 is lexicographically less than B2 B1 consist of: 01110011 B5 consist of: 00000000 B1 is lexicographically greater than B5 B3 is lexicographically less than or equal to B3 B5 consist of: 00000000 B2 consist of: 11001110 B5 is lexicographically less than B2 Value of dynamic_bitset B4 : 000 Creating flipped copy of B4: 111 Value of dynamic_bitset B2: 11001110 Copy of B2 left shift 3 times is : 01110000 Copy of B2 right shift 1 time is : 01100111 Value of dynamic_bitset B2: 11001110 B2 left shift 3 times is : 00111000 B2 right shift 1 time is : 00000111 Applications: dynamic_bitset can be effectively used to represent a subset of a finite set. Each bit represents whether an element of the finite set is in the subset or not.Sieve of Eratosthenes for finding all prime numbers below an integer N. Reference: https://www.boost.org/doc/libs/1_36_0/libs/dynamic_bitset/dynamic_bitset.html Comment More infoAdvertise with us Next Article Introduction to C++ Programming Language A abhisheksingh5275 Follow Improve Article Tags : C++ cpp-boost Practice Tags : CPP Similar Reads C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to 5 min read C++ OverviewIntroduction to C++ Programming LanguageC++ is a general-purpose programming language that was developed by Bjarne Stroustrup as an enhancement of the C language to add object-oriented paradigm. It is considered as a middle-level language as it combines features of both high-level and low-level languages. It has high level language featur 3 min read Features of C++C++ is a general-purpose programming language that was developed as an enhancement of the C language to include an object-oriented paradigm. It is an imperative and compiled language. C++ has a number of features, including:Object-Oriented ProgrammingMachine IndependentSimpleHigh-Level LanguagePopul 5 min read History of C++The C++ language is an object-oriented programming language & is a combination of both low-level & high-level language - a Middle-Level Language. The programming language was created, designed & developed by a Danish Computer Scientist - Bjarne Stroustrup at Bell Telephone Laboratories ( 7 min read Interesting Facts about C++C++ is a general-purpose, object-oriented programming language. It supports generic programming and low-level memory manipulation. Bjarne Stroustrup (Bell Labs) in 1979, introduced the C-With-Classes, and in 1983 with the C++. Here are some awesome facts about C++ that may interest you: The name of 2 min read Setting up C++ Development EnvironmentC++ runs on lots of platforms like Windows, Linux, Unix, Mac, etc. If you do not want to set up a local environment you can also use online IDEs for compiling your program.Using Online IDEIDE stands for an integrated development environment. IDE is a software application that provides facilities to 8 min read Difference between C and C++C++ is often viewed as a superset of C. C++ is also known as a "C with class" This was very nearly true when C++ was originally created, but the two languages have evolved over time with C picking up a number of features that either weren't found in the contemporary version of C++ or still haven't m 3 min read C++ BasicsUnderstanding First C++ ProgramThe "Hello World" program is the first step towards learning any programming language and is also one of the most straightforward programs you will learn. It is the basic program that demonstrates the working of the coding process. All you have to do is display the message "Hello World" on the outpu 4 min read C++ Basic SyntaxSyntax refers to the rules and regulations for writing statements in a programming language. They can also be viewed as the grammatical rules defining the structure of a programming language.The C++ language also has its syntax for the functionalities it provides. Different statements have different 4 min read C++ CommentsComments in C++ are meant to explain the code as well as to make it more readable. Their purpose is to provide information about code lines. When testing alternative code, they can also be used to prevent execution of some part of the code. Programmers commonly use comments to document their work.Ex 3 min read Tokens in CIn C programming, tokens are the smallest units in a program that have meaningful representations. Tokens are the building blocks of a C program, and they are recognized by the C compiler to form valid expressions and statements. Tokens can be classified into various categories, each with specific r 4 min read C++ KeywordsKeywords are the reserved words that have special meanings in the C++ language. They are the words that have special meaning in the language. C++ uses keywords for a specifying the components of the language, such as void, int, public, etc. They can't be used for a variable name, function name or an 2 min read Difference between Keyword and Identifier in CIn C, keywords and identifiers are basically the fundamental parts of the language used. Identifiers are the names that can be given to a variable, function or other entity while keywords are the reserved words that have predefined meaning in the language.The below table illustrates the primary diff 3 min read C++ Variables and ConstantsC++ VariablesIn C++, variable is a name given to a memory location. It is the basic unit of storage in a program. The value stored in a variable can be accessed or changed during program execution.Creating a VariableCreating a variable and giving it a name is called variable definition (sometimes called variable 4 min read Constants in CIn C programming, const is a keyword used to declare a variable as constant, meaning its value cannot be changed after it is initialized. It is mainly used to protect variables from being accidentally modified, making the program safer and easier to understand. These constants can be of various type 4 min read Scope of Variables in C++In C++, the scope of a variable is the extent in the code upto which the variable can be accessed or worked with. It is the region of the program where the variable is accessible using the name it was declared with.Let's take a look at an example:C++#include <iostream> using namespace std; // 7 min read Storage Classes in C++ with ExamplesC++ Storage Classes are used to describe the characteristics of a variable/function. It determines the lifetime, visibility, default value, and storage location which helps us to trace the existence of a particular variable during the runtime of a program. Storage class specifiers are used to specif 6 min read Static Keyword in C++The static keyword in C++ has different meanings when used with different types. In this article, we will learn about the static keyword in C++ along with its various uses.In C++, a static keyword can be used in the following context:Table of ContentStatic Variables in a FunctionStatic Member Variab 5 min read C++ Data Types and LiteralsC++ Data TypesData types specify the type of data that a variable can store. Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data type with which it is declared as every data type requires a different amount of memory.C++ supports a wide variety of data typ 7 min read Literals in CIn C, Literals are the constant values that are assigned to the variables. Literals represent fixed values that cannot be modified. Literals contain memory but they do not have references as variables. Generally, both terms, constants, and literals are used interchangeably. For example, âconst int = 4 min read Derived Data Types in C++The data types that are derived from the primitive or built-in datatypes are referred to as Derived Data Types. They are generally the data types that are created from the primitive data types and provide some additional functionality.In C++, there are four different derived data types:Table of Cont 4 min read User Defined Data Types in C++User defined data types are those data types that are defined by the user himself. In C++, these data types allow programmers to extend the basic data types provided and create new types that are more suited to their specific needs. C++ supports 5 user-defined data types:Table of ContentClassStructu 4 min read Data Type Ranges and Their Macros in C++Most of the times, in competitive programming, there is a need to assign the variable, the maximum or minimum value that data type can hold but remembering such a large and precise number comes out to be a difficult job. Therefore, C++ has certain macros to represent these numbers, so that these can 3 min read C++ Type ModifiersIn C++, type modifiers are the keywords used to change or give extra meaning to already existing data types. It is added to primitive data types as a prefix to modify their size or range of data they can store.C++ have 4 type modifiers which are as follows:Table of Contentsigned Modifierunsigned Mod 4 min read Type Conversion in C++Type conversion means converting one type of data to another compatible type such that it doesn't lose its meaning. It is essential for managing different data types in C++. Let's take a look at an example:C++#include <iostream> using namespace std; int main() { // Two variables of different t 4 min read Casting Operators in C++The casting operators is the modern C++ solution for converting one type of data safely to another type. This process is called typecasting where the type of the data is changed to another type either implicitly (by the compiler) or explicitly (by the programmer).Let's take a look at an example:C++# 5 min read C++ OperatorsOperators in C++C++ operators are the symbols that operate on values to perform specific mathematical or logical computations on given values. They are the foundation of any programming language.Example:C++#include <iostream> using namespace std; int main() { int a = 10 + 20; cout << a; return 0; }Outpu 9 min read C++ Arithmetic OperatorsArithmetic Operators in C++ are used to perform arithmetic or mathematical operations on the operands (generally numeric values). An operand can be a variable or a value. For example, â+â is used for addition, '-' is used for subtraction, '*' is used for multiplication, etc. Let's take a look at an 4 min read Unary Operators in CIn C programming, unary operators are operators that operate on a single operand. These operators are used to perform operations such as negation, incrementing or decrementing a variable, or checking the size of a variable. They provide a way to modify or manipulate the value of a single variable in 5 min read Bitwise Operators in CIn C, bitwise operators are used to perform operations directly on the binary representations of numbers. These operators work by manipulating individual bits (0s and 1s) in a number.The following 6 operators are bitwise operators (also known as bit operators as they work at the bit-level). They are 6 min read Assignment Operators in CIn C, assignment operators are used to assign values to variables. The left operand is the variable and the right operand is the value being assigned. The value on the right must match the data type of the variable otherwise, the compiler will raise an error.Let's take a look at an example:C#include 4 min read C++ sizeof OperatorThe sizeof operator is a unary compile-time operator used to determine the size of variables, data types, and constants in bytes at compile time. It can also determine the size of classes, structures, and unions.Let's take a look at an example:C++#include <iostream> using namespace std; int ma 3 min read Scope Resolution Operator in C++In C++, the scope resolution operator (::) is used to access the identifiers such as variable names and function names defined inside some other scope in the current scope. Let's take a look at an example:C++#include <iostream> int main() { // Accessing cout from std namespace using scope // r 4 min read C++ Input/OutputBasic Input / Output in C++In C++, input and output are performed in the form of a sequence of bytes or more commonly known as streams.Input Stream: If the direction of flow of bytes is from the device (for example, Keyboard) to the main memory then this process is called input.Output Stream: If the direction of flow of bytes 5 min read cin in C++In C++, cin is an object of istream class that is used to accept the input from the standard input stream i.e. stdin which is by default associated with keyboard. The extraction operator (>>) is used along with cin to extract the data from the object and insert it to the given variable.Let's t 4 min read cout in C++In C++, cout is an object of the ostream class that is used to display output to the standard output device, usually the monitor. It is associated with the standard C output stream stdout. The insertion operator (<<) is used with cout to insert data into the output stream.Let's take a look at 2 min read Standard Error Stream Object - cerr in C++In C++, cerr is the standard error stream used to output the errors. It is an instance of the ostream class and is un-buffered, so it is used when we need to display the error message immediately and does not store the error message to display later. The 'c' in cerr refers to "character" and 'err' m 3 min read Manipulators in C++Manipulators are helping functions that can modify the input or output stream. They can be included in the I/O statement to alter the format parameters of a stream. They are defined inside <iomanip> and some are also defined inside <iostream> header file. For example, if we want to print 4 min read C++ Control StatementsDecision Making in C (if , if..else, Nested if, if-else-if )In C, programs can choose which part of the code to execute based on some condition. This ability is called decision making and the statements used for it are called conditional statements. These statements evaluate one or more conditions and make the decision whether to execute a block of code or n 7 min read C++ if StatementThe C++ if statement is the most simple decision-making statement. It is used to decide whether a certain statement or block of statements will be executed or not executed based on a certain condition. Let's take a look at an example:C++#include <iostream> using namespace std; int main() { int 3 min read C++ if else StatementThe if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false, it wonât. But what if we want to do something else if the condition is false. Here comes the C++ if else statement. We can use the else statement with if statement to exec 3 min read C++ if else if LadderIn C++, the if-else-if ladder helps the user decide from among multiple options. The C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C++ else-if ladder is bypassed. I 3 min read Switch Statement in C++In C++, the switch statement is a flow control statement that is used to execute the different blocks of statements based on the value of the given expression. It is a simpler alternative to the long if-else-if ladder.SyntaxC++switch (expression) { case value_1: // code to be executed. break; case v 5 min read Jump statements in C++Jump statements are used to manipulate the flow of the program if some conditions are met. It is used to terminate or continue the loop inside a program or to stop the execution of a function.In C++, there is four jump statement:Table of Contentcontinue Statementbreak Statementreturn Statementgoto S 4 min read C++ LoopsIn C++ programming, sometimes there is a need to perform some operation more than once or (say) n number of times. For example, suppose we want to print "Hello World" 5 times. Manually, we have to write cout for the C++ statement 5 times as shown.C++#include <iostream> using namespace std; int 7 min read for Loop in C++In C++, for loop is an entry-controlled loop that is used to execute a block of code repeatedly for the given number of times. It is generally preferred over while and do-while loops in case the number of iterations is known beforehand.Let's take a look at an example:C++#include <bits/stdc++.h 6 min read Range-Based for Loop in C++In C++, the range-based for loop introduced in C++ 11 is a version of for loop that is able to iterate over a range. This range can be anything that is iteratable, such as arrays, strings and STL containers. It provides a more readable and concise syntax compared to traditional for loops.Let's take 3 min read C++ While LoopIn C++, the while loop is an entry-controlled loop that repeatedly executes a block of code as long as the given condition remains true. Unlike the for loop, while loop is used in situations where we do not know the exact number of iterations of the loop beforehand as the loop execution is terminate 3 min read C++ do while LoopIn C++, the do-while loop is an exit-controlled loop that repeatedly executes a block of code at least once and continues executing as long as a given condition remains true. Unlike the while loop, the do-while loop guarantees that the loop body will execute at least once, regardless of whether the 4 min read C++ FunctionsFunctions in C++A Function is a reusable block of code designed to perform a specific task. It helps break large programs into smaller, logical parts. Functions make code cleaner, easier to understand, and more maintainable.Just like in other languages, C++ functions can take inputs (called parameters), execute a b 8 min read return Statement in C++In C++, the return statement returns the flow of the execution to the function from where it is called. This statement does not mandatorily need any conditional statements. As soon as the statement is executed, the flow of the program stops immediately and returns the control from where it was calle 4 min read Parameter Passing Techniques in CIn C, passing values to a function means providing data to the function when it is called so that the function can use or manipulate that data. Here:Formal Parameters: Variables used in parameter list in a function declaration/definition as placeholders. Also called only parameters.Actual Parameters 3 min read Difference Between Call by Value and Call by Reference in CFunctions can be invoked in two ways: Call by Value or Call by Reference. These two ways are generally differentiated by the type of values passed to them as parameters.The following table lists the differences between the call-by-value and call-by-reference methods of parameter passing.Call By Valu 4 min read Default Arguments in C++A default argument is a value provided for a parameter in a function declaration that is automatically assigned by the compiler if no value is provided for those parameters in function call. If the value is passed for it, the default value is overwritten by the passed value.Example:C++#include <i 5 min read Inline Functions in C++In C++, inline functions provide a way to optimize the performance of the program by reducing the overhead related to a function call. When a function is specified as inline the whole code of the inline function is inserted or substituted at the point of its call during the compilation instead of us 6 min read Lambda Expression in C++C++ 11 introduced lambda expressions to allow inline functions which can be used for short snippets of code that are not going to be reused. Therefore, they do not require a name. They are mostly used in STL algorithms as callback functions.Example:C++#include <iostream> using namespace std; i 4 min read C++ Pointers and ReferencesPointers and References in C++In C++ pointers and references both are mechanisms used to deal with memory, memory address, and data in a program. Pointers are used to store the memory address of another variable whereas references are used to create an alias for an already existing variable. Pointers in C++ Pointers in C++ are a 5 min read C++ PointersA pointer is a special variable that holds the memory address of another variable, rather than storing a direct value itself. Pointers allow programs to access and manipulate data in memory efficiently, making them a key feature for system-level programming and dynamic memory management. When we acc 8 min read Dangling, Void , Null and Wild Pointers in CIn C programming pointers are used to manipulate memory addresses, to store the address of some variable or memory location. But certain situations and characteristics related to pointers become challenging in terms of memory safety and program behavior these include Dangling (when pointing to deall 6 min read Applications of Pointers in CPointers in C are variables that are used to store the memory address of another variable. Pointers allow us to efficiently manage the memory and hence optimize our program. In this article, we will discuss some of the major applications of pointers in C. Prerequisite: Pointers in C. C Pointers Appl 4 min read Understanding nullptr in C++Consider the following C++ program that shows problem with NULL (need of nullptr) CPP // C++ program to demonstrate problem with NULL #include <bits/stdc++.h> using namespace std; // function with integer argument void fun(int N) { cout << "fun(int)"; return;} // Overloaded fun 3 min read References in C++In C++, a reference works as an alias for an existing variable, providing an alternative name for it and allowing you to work with the original data directly.Example:C++#include <iostream> using namespace std; int main() { int x = 10; // ref is a reference to x. int& ref = x; // printing v 5 min read Can References Refer to Invalid Location in C++?Reference Variables: You can create a second name for a variable in C++, which you can use to read or edit the original data contained in that variable. While this may not sound appealing at first, declaring a reference and assigning it a variable allows you to treat the reference as if it were the 2 min read Pointers vs References in C++Prerequisite: Pointers, References C and C++ support pointers, which is different from most other programming languages such as Java, Python, Ruby, Perl and PHP as they only support references. But interestingly, C++, along with pointers, also supports references. On the surface, both references and 5 min read Passing By Pointer vs Passing By Reference in C++In C++, we can pass parameters to a function either by pointers or by reference. In both cases, we get the same result. So, what is the difference between Passing by Pointer and Passing by Reference in C++?Let's first understand what Passing by Pointer and Passing by Reference in C++ mean:Passing by 5 min read When do we pass arguments by pointer?In C, the pass-by pointer method allows users to pass the address of an argument to the function instead of the actual value. This allows programmers to change the actual data from the function and also improve the performance of the program. In C, variables are passed by pointer in the following ca 5 min read Like