-
Notifications
You must be signed in to change notification settings - Fork 2
/
find_largest_power_of_2.cpp
105 lines (82 loc) · 2.25 KB
/
find_largest_power_of_2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/****************************************************************************
File name: find_largest_power_of_2.cpp
Author: babajr
*****************************************************************************/
/*
Find the largest power of 2 (most significant bit in binary form),
which is less than or equal to the given number N.
Idea: Change all the bits which are at the right side of the most significant digit, to 1.
Input: 21
Output: 16
*/
#include <iostream>
#include <algorithm>
#include <cassert>
using namespace std;
/*Approach 1*/
/* Logic:
Let’s say N = 21 = {10101}, here most significant bit is the 4th one.
(counting from 0th digit) and so the answer should be 16.
So lets change all the right side bits of the most significant bit to 1.
Now the number changes to
{11111} = 31 = 2 * 16 -1 = Y (let’s say).
Now the required answer is (Y+1)>>1 or (Y+1)/2.
*/
long largest_power(long num)
{
// Change all right side bits of MSB set bit to 1.
num = num | (num >> 1);
num = num | (num >> 2);
num = num | (num >> 4);
num = num | (num >> 8);
// //as now the number is 2 * x-1, where x is required answer, so adding 1 and dividing it by 2.
return (num + 1) >> 1; // (num + 1) / 2
}
/*
Approach 2: TC = O(n)
Idea: start checking from num and keep decrementing until we find power of 2.
*/
int largest_power_1(int num)
{
int res = 0;
for(int i = num; i >= 1; i--)
{
// check if num is power of 2.
if((i & (i - 1)) == 0)
{
res = i;
break;
}
}
return res;
}
/*
Approach 3: TC = O(n)
Idea: Use bitwise left shift operator.
*/
int largest_power_2(int num)
{
if(num <= 0)
return 0;
int res = 1;
// assuming num is of 32 bits.
for(int i = 0; i < 32; i++)
{
int power_curr = 1 << i; // get the power of 2 for all the possible positions.
// if power_curr is more than input num, no need to check further.
if(power_curr > num)
break;
res = power_curr;
}
return res;
}
int main(void)
{
long num;
cout<<"Please Enter the number: ";
cin>>num;
printf("%d \n", largest_power(num));
printf("%d \n", largest_power_1(num));
printf("%d \n", largest_power_2(num));
return 0;
}