First discuss about what is bits toggling?
- Number 12 is represented as 1100 in binary format. Mathematical representation like (12)10 = (1100)2. So Number (1100)2 then complete toggled result is (0011)2, Here 1 is replaced with 0 and 0 with 1, this is called bitwise complement.
- But in some cases we need to toggle particular bit of Number. For example (1100)2 toggle 1st bit only, then result become (1110)2, (bit counted from right to left, counting started with number 0), this is single bit toggling.
- Now we can start with toggle bits of number with given range. For example (1100)2 toggle bits from 1st to 3rd bit, then result become (0010)2. This is rang bits toggling.
Bitwise complement:
Suppose N is unsigned integer value.
Then we can code in C like this:
Then we can code in C like this:
unsigned int bitwise_complement(unsigned int N) { return ~ N }
Operator ~ is used to get bitwise complement.
Single bit toggling:
Single bit toggle is possible with following method:
Input is (1100)2 and 2nd bit need to toggle, then first create binary number which contain 1 as bit value at 2nd bit. Suppose number is X, that we can create X like this 1 << 2 = (0100)2, Now do XOR of both number (1100)2 ^ (0100)2 = (1000)2
Then we can code in C like this:
Then we can code in C like this:
unsigned int single_bit_toggling(unsigned int N, unsigned int k) { return (N ^ (1 << k)); }
Toggle a given range of bits:
Input is (1100)2 and range is from 1st bit to 3rd bit. Then first create binary number which contain 1 as a bit value from 1st bit to 3rd bit and other bits values are 0. So required value is (1110)2. But how to created (1110)2 using given rang ? So first find range difference i.e. 3 -1 = 2, then (1 << (2+1)) -1 = (7)10 = (0111)2 , Now shift this value with lowest rang value (0111)2 << 1 = (1110)2, This is required value to do XOR with input
(1100)2 ^ (1110)2 = (0010)2
Then we can code in C like this:
unsigned int rang_toggling(unsigned int N, unsigned int k , unsigned int l) { //Here l > k return N ^ ((1<<(l-k)+1)-1)<<k; }
No comments:
Post a Comment