-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitpos.cpp
More file actions
38 lines (29 loc) · 845 Bytes
/
bitpos.cpp
File metadata and controls
38 lines (29 loc) · 845 Bytes
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
// Author: Kiran Chinta
// Prints the set bit positions for a given 64bit number.
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(int argc, char** argv)
{
if (argc != 2)
{
cout << "Incorrect number of arguments" << endl;
cout << "Given a number(less than 9223372036854775808), outputs the bit position(s) set in binary format for that decimal number" << endl;
cout << "Usage: " << argv[0] << " DecimalNumber" << endl;
return 0;
}
char * end;
unsigned long long num = strtoull(argv[1], &end, 10);
cout << "Input : " << num << endl;
cout << "Binary: " << std::bitset<64>(num) << endl;
int pos = 0;
while( num != 0 )
{
if (( num & 1) == 1)
{
cout << "Bit position: " << pos << " is set" << endl;
}
num >>= 1;
pos++;
}
}