-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrevert_int32.cpp
More file actions
36 lines (32 loc) · 882 Bytes
/
Copy pathrevert_int32.cpp
File metadata and controls
36 lines (32 loc) · 882 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
#include <cassert>
#include <iostream>
#include <vector>
int revert_int(int _num) {
// integer number bins length <= 32
std::vector<bool> bin2(32, false);
int counter = 0;
int num = _num;
while (num) {
// std::cout<< " NUM:" << () << std::endl;
bin2[counter++] = (num & 0x01 == 0x01);
num >>= 1;
}
std::cout << "THE " << _num << " COUNTER : " << counter << std::endl;
for (int i = 0; i < counter; ++i) {
std::cout << bin2[i];
}
std::cout << std::endl;
int result = bin2[0];
for (int i = 1; i < counter; ++i) {
result <<= 1;
result |= bin2[i] ? 0x01 : 0x00;
// std::cout << i << " : "<< bin2[i]<< " : " << result << std::endl;
}
return result;
}
int main(int argc, char const *argv[]) {
assert(revert_int(9) == 9);
assert(revert_int(8) == 1);
assert(revert_int(6) == 3);
return 0;
}