Bitwise

Algorithm Algorithm Gist
Counting number of set bits Counting set bits using bitset.
Counting set bits using __builtin_popcount
Counting set bits using Logical AND (&) and Right Shift (») operators
Swapping integers using XOR operation XOR stands for exclusive or. XOR opertion of two bits results in 1 only if the bits are different; for the same bits XOR operation results in 0.
Converting a binary string into its equivalent decimal number Starting with the least significant (rightmost) bit we multiple each binary digit with the power of 2 starting with 0. As we move towards the most significant (leftmost) bit we increase the power of 2 by 1. The resultant decimal number is the addition of all the multiplications.
Example : Binary string (0110)
resultant decimal = 0 . 23 + 1 . 22 + 1 . 21 + 0 . 20
resultant decimal = 6


Copyright (c) 2019-2024, Algotree.org.
All rights reserved.