Extract Bits in C++ Last Updated : 08 Jul, 2024 Comments Improve Suggest changes Like Article Like Report Extracting bits from a given number involves extracting 'k' bits starting from a specified position 'pos' by using bitwise operations like AND (&) and shifts (<<, >>). In this article, we will learn how to extract bits in C++.Example:Input:num = 214k = 3pos = 2Output:5Extracting Bits in C++To extract k bits from a given position pos in a number, first perform right shift on num by pos bits that brings the target bits to the least significant positions, then create a mask with k bits set to 1 and apply it to shifted number (that we got after performing right shift operation) using the AND operation.Steps to Extract BitsConsider the above example, where num=214 (binary: 11010110), k=3 and pos=2.Right Shift: shifted = num >> posshifted = 214 >> 2 = 53 (binary: 110101)Create Mask: mask = (1 << k) - 1mask = (1<<3) - 1 = 7 (binary 111) Apply Mask: result = shifted & maskresult = 53 & 7 = 5 (binary: 101)C++ Program to Extract in a NumberThe below program demonstrates how we can extract âkâ bits from a given position âpâ in a number. C++ // C++ program to extract 'k' bits from position 'pos' in // number 'num' #include <iostream> using namespace std; // Function to extract 'k' bits from position 'pos' in // number 'num' unsigned int extractBits(unsigned int num, unsigned int pos, unsigned int k) { // Right shift 'num' by 'pos' bits unsigned int shifted = num >> pos; // Create a mask with 'k' bits set to 1 unsigned int mask = (1 << k) - 1; // Apply the mask to the shifted number return shifted & mask; } int main() { // Example number from which bits are to be extracted unsigned int num = 214; // Starting position of bits to extract unsigned int pos = 2; // Number of bits to extract unsigned int k = 3; // Call the function to extract bits unsigned int result = extractBits(num, pos, k); // Print the result cout << "Extracted bits: " << result << endl; return 0; } OutputExtracted bits: 7 Time Complexity: O(1), as each operation within the extractBits function (right shift, mask creation, and bitwise AND) is performed in constant time.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Extract Bits in C++ A ankushmispc9l Follow Improve Article Tags : C++ C++ Bit Manipulation CPP-DSA Practice Tags : CPP Similar Reads Extract bits in C In C programming, extracting a bit means retrieving the value (0 or 1) of a bit or a group of bits present at a specific position in the binary representation of a numberIn this article, we will learn how to extract a bit or multiple bits at given positions in a binary number. We will also explore h 3 min read string at() in C++ The std::string::at() in C++ is a built-in function of std::string class that is used to extract the character from the given index of string. In this article, we will learn how to use string::at() in C++.Syntaxstr.at(idx)Parametersidx: Index at which we have to find the character.Return ValueReturn 1 min read Extract all integers from string in C++ Given a string, extract all integers words from it. Examples : Input : str = "geeksforgeeks 12 13 practice" Output : 12 13 Input : str = "1: Prakhar Agrawal, 2: Manish Kumar Rai, 3: Rishabh Gupta" Output : 1 2 3 Input : str = "Ankit sleeps at 4 am." Output : 4 The idea is to use stringstream:, objec 2 min read basic_istream::unget() in C++ with Examples The basic_istream::unget() is used to unget the character and used to decrease the location by one character and makes the extracted character available for used once again. Header File: <iostream> Syntax: basic_istream& unget(); Parameters: The basic_istream::unget() method doesnât accept 2 min read cin get() in C++ with Examples cin.get() is used for accessing character array. It includes white space characters. Generally, cin with an extraction operator (>>) terminates when whitespace is found. However, cin.get() reads a string with the whitespace. Syntax: cin.get(string_name, size); Example 1: CPP // C++ program to 1 min read Like