Skip to content

Commit aa780a7

Browse files
Add more bits manipulation
1 parent 5d7ba26 commit aa780a7

21 files changed

+791
-16
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
## Swap bits in a given number
2+
3+
Given a number x and two positions (from the right side) in the binary representation of x, write a function that swaps n bits at given two positions and returns the result. It is also given that the two sets of bits do not overlap.
4+
5+
1) Move all bits of the first set to the rightmost side
6+
set1 = (x >> p1) & ((1U << n) - 1)
7+
Here the expression (1U << n) - 1 gives a number that
8+
contains last n bits set and other bits as 0. We do &
9+
with this expression so that bits other than the last
10+
n bits become 0.
11+
12+
2) Move all bits of second set to rightmost side
13+
set2 = (x >> p2) & ((1U << n) - 1)
14+
15+
3) XOR the two sets of bits
16+
xor = (set1 ^ set2)
17+
18+
4) Put the xor bits back to their original positions.
19+
xor = (xor << p1) | (xor << p2)
20+
21+
5) Finally, XOR the xor with original number so
22+
that the two sets are swapped.
23+
result = x ^ xor
24+
25+
```c
26+
#include <stdio.h>
27+
// C program to swap bits in an integer
28+
#include<stdio.h>
29+
30+
// This function swaps bit at positions p1 and p2 in an integer n
31+
int swapBit(unsigned int n, unsigned int p1, unsigned int p2)
32+
{
33+
/* Move p1'th to rightmost side */
34+
unsigned int bit1 = (n >> p1) & 1;
35+
36+
/* Move p2'th to rightmost side */
37+
unsigned int bit2 = (n >> p2) & 1;
38+
39+
/* XOR the two bits */
40+
unsigned int x = (bit1 ^ bit2);
41+
42+
/* Put the xor bit back to their original positions */
43+
x = (x << p1) | (x << p2);
44+
45+
/* XOR 'x' with the original number so that the
46+
two sets are swapped */
47+
unsigned int result = n ^ x;
48+
}
49+
50+
/* Driver program to test above function*/
51+
int main()
52+
{
53+
int res = swapBits(28, 0, 3);
54+
printf("Result = %d ", res);
55+
return 0;
56+
}
57+
58+
int swapBits(unsigned int x, unsigned int p1, unsigned int p2, unsigned int n)
59+
{
60+
/* Move all bits of first set to rightmost side */
61+
unsigned int set1 = (x >> p1) & ((1U << n) - 1);
62+
63+
/* Move all bits of second set to rightmost side */
64+
unsigned int set2 = (x >> p2) & ((1U << n) - 1);
65+
66+
/* XOR the two sets */
67+
unsigned int xor = (set1 ^ set2);
68+
69+
/* Put the xor bits back to their original positions */
70+
xor = (xor << p1) | (xor << p2);
71+
72+
/* XOR the 'xor' with the original number so that the
73+
two sets are swapped */
74+
unsigned int result = x ^ xor;
75+
76+
return result;
77+
}
78+
79+
/* Driver program to test above function*/
80+
int main()
81+
{
82+
int res = swapBits(28, 0, 3, 2);
83+
printf("\nResult = %d ", res);
84+
return 0;
85+
}
86+
```
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
## Check if binary representation of a number is palindrome
2+
3+
Given an integer ‘x’, write a C function that returns true if binary representation of x is palindrome else return false.
4+
For example a numbers with binary representation as 10..01 is palindrome and number with binary representation as 10..00 is not palindrome.
5+
6+
The idea is similar to checking a string is palindrome or not. We start from leftmost and rightmost bits and compare bits one by one. If we find a mismatch, then return false.
7+
8+
```c
9+
#include<stdio.h>
10+
#include<stdint.h>
11+
12+
typedef uint32_t (*generalTestFunct)(uint32_t target);
13+
14+
uint32_t isBinaryPanlidrome(uint32_t target) {
15+
int i = 0;
16+
for (i; i < 32; i++) {
17+
if (((target>>i) & 0x1) != ((target>>(32-1-i)) & 0x1))
18+
return 0;
19+
}
20+
21+
return 1;
22+
}
23+
24+
int main(void) {
25+
generalTestFunct test_func1 = isBinaryPanlidrome;
26+
27+
// test 1:
28+
uint32_t test_num1 = 0;
29+
printf("%d\n", test_func1(test_num1));
30+
31+
// test 2:
32+
test_num1 = 0xf000000f;
33+
printf("%d\n", test_func1(test_num1));
34+
35+
// test 3:
36+
test_num1 = 0x11001100;
37+
printf("%d\n", test_func1(test_num1));
38+
39+
// test 4:
40+
test_num1 = 0xa0000005;
41+
printf("%d\n", test_func1(test_num1));
42+
43+
return 0;
44+
}
45+
```
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## Count number of bits to be flipped to convert A to B
2+
3+
Given two numbers ‘a’ and b’. Write a program to count number of bits needed to be flipped to convert ‘a’ to ‘b’.
4+
5+
```c
6+
#include<stdio.h>
7+
#include<stdint.h>
8+
9+
typedef uint32_t (*generalTestFunct)(uint32_t num1, uint32_t num2);
10+
11+
uint32_t flipBitsNumber(uint32_t num1, uint32_t num2) {
12+
num1 ^= num2;
13+
14+
int res = 0;
15+
while(num1) {
16+
res ++;
17+
num1 &= num1-1;
18+
}
19+
20+
return res;
21+
}
22+
23+
int main(void) {
24+
generalTestFunct test_func1 = flipBitsNumber;
25+
26+
// test 1:
27+
uint32_t test_num1 = 1;
28+
uint32_t test_num2 = 1;
29+
printf("%d\n", test_func1(test_num1, test_num2));
30+
31+
// test 2:
32+
test_num1 = 0xffff0000;
33+
test_num2 = 0x1;
34+
printf("%d\n", test_func1(test_num1, test_num2));
35+
36+
// test 3:
37+
test_num1 = 0x11001100;
38+
test_num2 = 1;
39+
printf("%d\n", test_func1(test_num1, test_num2));
40+
41+
// test 4:
42+
test_num1 = 0x00110011;
43+
test_num2 = 1;
44+
printf("%d\n", test_func1(test_num1, test_num2));
45+
46+
return 0;
47+
}
48+
```
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
```c
2+
#include<stdio.h>
3+
#include<stdint.h>
4+
5+
// Task: Find Bit position of the only set bit
6+
// Tips: right shift until the number becomes zero. Be careful that the index starts with -1 instead of 0!
7+
typedef int (*generalTestFunct)(uint32_t target);
8+
9+
int setBit_pos(uint32_t target) {
10+
int pos = -1;
11+
while (target) {
12+
pos ++;
13+
target >>= 1;
14+
}
15+
16+
return pos;
17+
}
18+
19+
int main(void) {
20+
generalTestFunct test_func = setBit_pos;
21+
22+
// test 1:
23+
uint32_t test1 = 0x1;
24+
printf("%x\n", test_func(test1));
25+
26+
// test 2:
27+
uint32_t test2 = 0x8;
28+
printf("%x\n", test_func(test2));
29+
30+
// test 3:
31+
uint32_t test3 = 0x80;
32+
printf("%x\n", test_func(test3));
33+
34+
// test 4:
35+
uint32_t test4 = 0x10000;
36+
printf("%x\n", test_func(test4));
37+
38+
return 0;
39+
}
40+
```
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## Reverse Bits
2+
3+
```c
4+
#include<stdio.h>
5+
#include<stdint.h>
6+
7+
typedef uint32_t (*generalTestFunct)(uint32_t target);
8+
9+
uint32_t reverseBits(uint32_t target) {
10+
target = ((target & 0xffff0000) >> 16) | ((target & 0x0000ffff) << 16);
11+
target = ((target & 0xff00ff00) >> 8) | ((target & 0x00ff00ff) << 8);
12+
target = ((target & 0xf0f0f0f0) >> 4) | ((target & 0x0f0f0f0f) << 4);
13+
target = ((target & 0xcccccccc) >> 2) | ((target & 0x33333333) << 2);
14+
target = ((target & 0xaaaaaaaa) >> 1) | ((target & 0x55555555) << 1);
15+
16+
return target;
17+
}
18+
19+
int main(void) {
20+
generalTestFunct test_func1 = reverseBits;
21+
22+
// test 1:
23+
uint32_t test_num1 = 1;
24+
printf("%x\n", test_func1(test_num1));
25+
26+
// test 2:
27+
test_num1 = 0xffff0000;
28+
printf("%x\n", test_func1(test_num1));
29+
30+
// test 3:
31+
test_num1 = 0x11001100;
32+
printf("%x\n", test_func1(test_num1));
33+
34+
// test 4:
35+
test_num1 = 0x00110011;
36+
printf("%x\n", test_func1(test_num1));
37+
38+
return 0;
39+
}
40+
```
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
## Rotate bits of a number
2+
3+
Bit Rotation: A rotation (or circular shift) is an operation similar to shift except that the bits that fall off at one end are put back to the other end.
4+
In left rotation, the bits that fall off at left end are put back at right end.
5+
6+
In right rotation, the bits that fall off at right end are put back at left end.
7+
8+
```c
9+
#include<stdio.h>
10+
#include<stdint.h>
11+
12+
// Task: Turn off the rightmost set bit
13+
typedef uint32_t (*generalTestFunct)(uint32_t target, int pos);
14+
15+
uint32_t leftRotate(uint32_t target, int pos) {
16+
return (target >> (32 - (pos%32))) | (target << (pos%32));
17+
}
18+
19+
uint32_t rightRotate(uint32_t target, int pos) {
20+
return (target >> (pos%32)) | (target << (32-(pos%32)));
21+
}
22+
23+
int main(void) {
24+
generalTestFunct test_func1 = leftRotate;
25+
generalTestFunct test_func2 = rightRotate;
26+
27+
// test 1:
28+
uint32_t test_num1 = 1;
29+
int shift = 33;
30+
printf("%x\n", test_func1(test_num1, shift));
31+
printf("%x\n", test_func2(test_num1, shift));
32+
33+
// test 2:
34+
test_num1 = 0x1000;
35+
shift = 1;
36+
printf("%x\n", test_func1(test_num1, shift));
37+
printf("%x\n", test_func2(test_num1, shift));
38+
39+
// test 3:
40+
test_num1 = 0x1100;
41+
shift = 1;
42+
printf("%x\n", test_func1(test_num1, shift));
43+
printf("%x\n", test_func2(test_num1, shift));
44+
45+
// test 4:
46+
test_num1 = 0x100010;
47+
shift = 1;
48+
printf("%x\n", test_func1(test_num1, shift));
49+
printf("%x\n", test_func2(test_num1, shift));
50+
51+
return 0;
52+
}
53+
```
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
## Detect if two integers have opposite signs
2+
3+
Given two signed integers, write a function that returns true if the signs of given integers are different, otherwise false. For example, the function should return true -1 and +100, and should return false for -100 and -200. The function should not use any of the arithmetic operators.
4+
5+
```c++
6+
#include<stdio.h>
7+
#include<stdint.h>
8+
9+
// Task: Find position of the only set bit
10+
typedef int (*generalTestFunct)(int target, int target2);
11+
12+
int checkSignDiff(int num1, int num2) {
13+
return (num1 ^ num2) < 0;
14+
}
15+
16+
int main(void) {
17+
generalTestFunct test_func = checkSignDiff;
18+
19+
// test 1:
20+
int test_num1 = 1;
21+
int test_num2 = -1;
22+
printf("%x\n", test_func(test_num1, test_num2));
23+
24+
// test 2:
25+
test_num1 = 1;
26+
test_num2 = 1;
27+
printf("%x\n", test_func(test_num1, test_num2));
28+
29+
// test 3:
30+
test_num1 = 2121312;
31+
test_num2 = 2121;
32+
printf("%x\n", test_func(test_num1, test_num2));
33+
34+
// test 4:
35+
test_num1 = -213;
36+
test_num2 = 21443212;
37+
printf("%x\n", test_func(test_num1, test_num2));
38+
39+
return 0;
40+
}
41+
```

0 commit comments

Comments
 (0)