-
Notifications
You must be signed in to change notification settings - Fork 14
/
Closest_int_same_bits.cpp
96 lines (87 loc) · 2.12 KB
/
Closest_int_same_bits.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
// Copyright (c) 2013 Elements of Programming Interviews. All rights reserved.
#include <cassert>
#include <iostream>
#include <limits>
#include <random>
#include <stdexcept>
using std::cout;
using std::default_random_engine;
using std::endl;
using std::exception;
using std::invalid_argument;
using std::numeric_limits;
using std::random_device;
using std::uniform_int_distribution;
int set_bit(int x, int bit, int set_value) {
return set_value ? x | (1 << bit) : x & ~(1 << bit);
}
int search(int x, int set_value, int default_value) {
int index = 0, num = 0;
while (((x >> index) & 1) != set_value) {
++index;
}
while (((x >> index) & 1) == set_value) {
++index;
++num;
}
if (index == 32) {
return default_value;
} else {
x ^= (1 << index);
--index;
--num;
for (int i = index; i >= num; --i) {
x = set_bit(x, i, !set_value);
}
for (int i = num - 1; i >= 0; --i) {
x = set_bit(x, i, set_value);
}
return x;
}
}
/*
int closest_int_same_bits(int x) {
int prev = search(x, 0, INT_MIN);
int next = search(x, 1, numeric_limits<int>::max());
return abs(x - prev) < abs(x - next) ? prev : next;
}
*/
// @include
unsigned long closest_int_same_bits(unsigned long x) {
for (int i = 0; i < 63; ++i) {
if (((x >> i) & 1) ^ ((x >> (i + 1)) & 1)) {
x ^= (1UL << i) | (1UL << (i + 1)); // swaps bit-i and bit-(i + 1).
return x;
}
}
// Throw error if all bits of x are 0 or 1.
throw invalid_argument("all bits are 0 or 1");
}
// @exclude
int count_bits_set_to_1(int x) {
int count = 0;
while (x) {
x &= (x - 1);
++count;
}
return count;
}
int main(int argc, char* argv[]) {
default_random_engine gen((random_device())());
unsigned long x;
if (argc == 2) {
x = atol(argv[1]);
} else {
uniform_int_distribution<int> dis(0, numeric_limits<int>::max());
x = dis(gen);
}
try {
unsigned long res = closest_int_same_bits(x);
cout << x << ' ' << res << endl;
assert(count_bits_set_to_1(x) == count_bits_set_to_1(res));
}
catch (const exception& e) {
cout << x << ' ' << e.what() << endl;
}
return 0;
}