-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday4.cpp
More file actions
81 lines (60 loc) · 2.01 KB
/
day4.cpp
File metadata and controls
81 lines (60 loc) · 2.01 KB
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
#include <iostream>
using namespace std;
// bitwise operator
/*
a&b -> this compares the bits of each number and returns 1 only if both are 1,
a|b -> returns 1 only if any of the bit is 1,
a^b -> 1 only if only one bit is 1,
right (>>) and left shift (<<) operator, shifts the bit as directed
global and local scopes (variable) means the way we defined the variale, inside a function/block -> local scope, whole code, whole section -> global scope
data types:
int -> limited value upto : -(2)^31 to (2)^31 - 1 , 4bytes : 32bits
Type | Size | Value Range (Approximate)
-------------|----------|-------------------------------
short | 2 bytes | –(2)^15 to (2)^15 – 1
int | 4 bytes | –(2)^31 to (2)^31 – 1
long | 4 or 8 B | ≥ int, often same as or larger than int
long long | 8 bytes | –(2)^63 to (2)^63 – 1
signed | — | Same as signed int (allows negatives)
unsigned | — | 0 to (2^n) – 1 (only positive values)
*/
bool checkP2(int n)
{
if (n <= 0)
return false;
while (n % 2 == 0)
n /= 2;
return n == 1;
// optimised way : return (n > 0) && ((n & (n - 1)) == 0);
}
int numReverse(int n)
{
int ans = 0;
while (n > 0)
{
int rem = n % 10;
n /= 10;
ans = ans * 10 + rem;
}
return ans;
}
int main()
{
// int a = 4, b = 6;
// cout << (a & b) << endl;
// cout << "left shifting a 2 times : " << (a << 2) << endl;
// cout << "right shifting a 2 times : " << (a >> 2) << endl;
int c;
cout << "Enter a number : ";
cin >> c;
// if (checkP2(c))
// {
// cout << "Number is a power of 2" << endl;
// }
// else
// {
// cout << "Number is NOT a power of 2" << endl;
// }
cout << "org num : " << c << " reversed : " << (numReverse(c)) << endl;
return 0;
}