Skip to content

Commit 7a4e193

Browse files
committed
Day-25: Bit manipulation problems
1 parent 159cd6e commit 7a4e193

File tree

4 files changed

+169
-2
lines changed

4 files changed

+169
-2
lines changed

README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
This repository will contain random algorithm and data structure problems I resolve to solve at least one a day.
33

44
## Current Streak
5-
**23 days**
5+
**25 days**
66
## Longest streak
7-
23 days (August 17, 2015 - Sept 08, 2015)
7+
25 days (August 17, 2015 - Sept 10, 2015)
88

99
## Include Directiory
1010
Include directory and sub directories contain STL like header file implementation of various algorithms and data structures. Following header only implementation,
@@ -52,3 +52,6 @@ please let me know.
5252
### Bit Manipulation
5353
- Determine if a number is a power of 2.
5454
- Add two binary number represented as string.
55+
- Determine the next power of 2 for a given number.
56+
- Using bit manipulation determine if a number is multiple of 3.
57+
- Determine endianess of the machine, print a number in reverse Endianess.

bit_manipulation/multiple_of_3.cpp

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Check if a given number is multiple of 3
3+
* Basic way of doing is : if sum of digits of number is multiple of 3,
4+
* number is divisible by 3.
5+
*
6+
* However another efficient way of doing it is:
7+
* Count the number of set bits at even positions.
8+
* Count the number of set bits at odd positions.
9+
* if difference is multiple of 3, number will be multiple of 3.
10+
*
11+
* Proof:
12+
* We can prove it by taking the example of 11 in decimal numbers.
13+
* (It will apply to 3 in binary numbers as well.)
14+
* AB = 10A + B ( A and B are digits of number AB)
15+
* AB = 11A + (B - A)
16+
* Clearly if (B-A) is multiple of 11, number would be muliple of 11.
17+
* We can do it similarly for 3 digits
18+
* ABC = 100A + 10B + C
19+
* = 99A + A + 11B - B + C
20+
* = 11(9A + B) + (A - B + C)
21+
* Clearly if A-B+C is multiple of 11, ABC would be multiple as well.
22+
* similarly for 4 digits
23+
* ABCD = 1000A + 100B + 10C + D
24+
* = (1001A – 999B + 11C) + (D + B – A -C )
25+
* So, if (B + D – A – C) is a multiple of 11 then is ABCD.
26+
*/
27+
28+
#include <iostream>
29+
30+
bool is_multiple_of_3( int n )
31+
{
32+
// change to positive if negative
33+
if ( n < 0 ) {
34+
n = -n;
35+
}
36+
if ( n == 0 ) {
37+
return true;
38+
}
39+
if ( n == 1 ) {
40+
return false;
41+
}
42+
int even_count = 0;
43+
int odd_count = 0;
44+
while ( n ) {
45+
if ( n & 1 ) {
46+
++odd_count;
47+
}
48+
n >>= 1;
49+
if ( n & 1 ) {
50+
++even_count;
51+
}
52+
n >>= 1;
53+
}
54+
return is_multiple_of_3( even_count - odd_count );
55+
}
56+
57+
int main()
58+
{
59+
int num;
60+
std::cout << "Enter a number:";
61+
std::cin >> num;
62+
if (is_multiple_of_3(num)) {
63+
std::cout << num << " is multiple of 3" << std::endl;
64+
} else {
65+
std::cout << num << " is not a multiple of 3" << std::endl;
66+
}
67+
return 0;
68+
}

bit_manipulation/next_power_of_2.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Write a function that, for a given no n, finds a number p which is greater than or equal to n and is a power of 2.
3+
*/
4+
#include <iostream>
5+
6+
int next_power_of_2( int num ) {
7+
//already a power of 2
8+
if (num && !(num & (num-1))) {
9+
return num;
10+
}
11+
//count till msb set bit
12+
int count = 0;
13+
while ( num != 0 ) {
14+
num >>= 1;
15+
count++;
16+
}
17+
return (1 << count);
18+
}
19+
20+
int main()
21+
{
22+
std::cout << "Enter a number:";
23+
int num;
24+
std::cin >> num;
25+
std::cout << "Next power of 2 which is greater than or equal to " << num
26+
<< " is: " << next_power_of_2(num) << std::endl;
27+
return 0;
28+
}
+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <iostream>
2+
#include <cstdio>
3+
4+
enum endianess {
5+
LITTLE_ENDIAN_MACHINE = 0,
6+
BIG_ENDIAN_MACHINE
7+
};
8+
9+
endianess determine_endianess()
10+
{
11+
unsigned int num = 1;
12+
char * c = (char *) &num;
13+
if (*c == 1) {
14+
return LITTLE_ENDIAN_MACHINE;
15+
} else {
16+
return BIG_ENDIAN_MACHINE;
17+
}
18+
}
19+
20+
void printBytes( char * start, int size)
21+
{
22+
for ( int i = 0; i < size; ++i ) {
23+
printf("%.2x ", start[i] );
24+
}
25+
std::cout << std::endl;
26+
}
27+
28+
int reverseEndianNess( int num )
29+
{
30+
int byte1, byte2, byte3, byte4;
31+
byte1 = (num & 0x000000FF) >> 0;
32+
byte2 = (num & 0x0000FF00) >> 8;
33+
byte3 = (num & 0x00FF0000) >> 16;
34+
byte4 = (num & 0xFF000000) >> 24;
35+
return ((byte1 << 24) | (byte2 << 16) | (byte3 << 8) | (byte4 << 0));
36+
}
37+
38+
int main() {
39+
endianess sys_endianess = determine_endianess();
40+
if (sys_endianess == LITTLE_ENDIAN_MACHINE) {
41+
std::cout << "System is little endian\n\n";
42+
} else {
43+
std::cout << "System is big endian\n\n";
44+
}
45+
46+
int num = 0x01234567;
47+
std::cout << "Num in decimal: " << num << std::endl;
48+
std::ios::fmtflags f(std::cout.flags());
49+
std::cout << "Num in hexadecimal:" << std::hex << num << std::endl;
50+
std::cout.flags( f );
51+
std::cout << "Printing individual bytes:\n";
52+
printBytes((char*)&num, sizeof(num));
53+
54+
std::cout << std::endl;
55+
std::cout << "Num in reversed endianness:\n";
56+
int num1 = reverseEndianNess(num);
57+
58+
std::cout << "Num in decimal :" << num1 << std::endl;
59+
60+
f = std::cout.flags();
61+
std::cout << "Num in hexadecimal:" << std::hex << num1 << std::endl;
62+
std::cout.flags( f );
63+
std::cout << "Printing individual bytes:\n";
64+
printBytes((char*)&num1, sizeof(num1));
65+
66+
return 0;
67+
68+
}

0 commit comments

Comments
 (0)