Below you will find pages that utilize the taxonomy term “BITMANIPULATION”
Posts
Bitwise Operation
Logically, a bit-to-bit operation is a calculation manipulating the data directly at the level of the bits, according to a Boolean arithmetic
Use in Programming Advanced to be blazingly fast
OPERATORS
read morePosts
Is Power Of Two
PROBLEM Given an integer n, return true if it is a power of two. Otherwise, return false.
An integer n is a power of two, if there exists an integer x such that n = 2x.=
Example1 *Input*: n = 1 *Output*: true Explanation: 20 = 1 Example2 *Input*: n = 16 *Output*: true Explanation: 24 = 16 Example3 *Input*: n = 3 *Output*: false SOLVING we’ll use Bit manipulation method.
read morePosts
Reverse Bits
PROBLEM Reverse bits of a given 32 bits unsigned integer.
SOLVING We’ll use Bit Manipulation method
Steps Create a uint32_t result we’ll return as the answer Repeat 32 time or the longer of your bit result equal to result or (| or bit operation) on the result of n and (& and bit operation) 1 to get the first bit Example Example with 6bit (32 is tooooo long) 011101
=n= [011101] & 1 == 1 =result= [000000] << 1 =n= [001110] & 1 == 0 =result= [000001] << 0 =n= [000111] & 1 == 1 =result= [000010] << 1 =n= [000011] & 1 == 1 =result= [000101] << 1 =n= [000001] & 1 == 1 =result= [001011] << 1 =n= [000000] & 1 == 0 =result= [010111] << 0 =result= [101110] Code class Solution { public: uint32_t reverseBits(uint32_t n) { uint32_t result; for (int i = 0; i < 32; i++) { result = (result << 1) | (n & 1); n >>= 1; } return result; } };
read more