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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
use num::basic::signeds::PrimitiveSigned;
use num::basic::unsigneds::PrimitiveUnsigned;
use num::logic::traits::BitAccess;

fn get_bit_unsigned<T: PrimitiveUnsigned>(x: &T, index: u64) -> bool {
    index < T::WIDTH && *x & T::power_of_2(index) != T::ZERO
}

fn set_bit_unsigned<T: PrimitiveUnsigned>(x: &mut T, index: u64) {
    if index < T::WIDTH {
        *x |= T::power_of_2(index);
    } else {
        panic!(
            "Cannot set bit {} in non-negative value of width {}",
            index,
            T::WIDTH
        );
    }
}

fn clear_bit_unsigned<T: PrimitiveUnsigned>(x: &mut T, index: u64) {
    if index < T::WIDTH {
        *x &= !T::power_of_2(index);
    }
}

macro_rules! impl_bit_access_unsigned {
    ($t:ident) => {
        impl BitAccess for $t {
            /// Determines whether the $i$th bit of an unsigned primitive integer, or the
            /// coefficient of $2^i$ in its binary expansion, is 0 or 1.
            ///
            /// `false` means 0 and `true` means 1. Getting bits beyond the type's width is allowed;
            /// those bits are false.
            ///
            /// Let
            /// $$
            /// n = \sum_{i=0}^\infty 2^{b_i},
            /// $$
            /// where for all $i$, $b_i\in \\{0, 1\\}$; so finitely many of the bits are 1, and the
            /// rest are 0. Then $f(n, j) = (b_j = 1)$.
            ///
            /// # Worst-case complexity
            /// Constant time and additional memory.
            ///
            /// # Examples
            /// See [here](super::bit_access#get_bit).
            #[inline]
            fn get_bit(&self, index: u64) -> bool {
                get_bit_unsigned(self, index)
            }

            /// Sets the $i$th bit of an unsigned primitive integer, or the coefficient of $2^i$ in
            /// its binary expansion, to 1.
            ///
            /// Setting bits beyond the type's width is disallowed.
            ///
            /// Let
            /// $$
            /// n = \sum_{i=0}^{W-1} 2^{b_i},
            /// $$
            /// where for all $i$, $b_i\in \\{0, 1\\}$, and $W$ is the width of the type. Then
            /// $$
            /// n \gets \\begin{cases}
            ///     n + 2^j & \text{if} \\quad b_j = 0, \\\\
            ///     n & \text{otherwise},
            /// \\end{cases}
            /// $$
            /// where $j < W$.
            ///
            /// # Worst-case complexity
            /// Constant time and additional memory.
            ///
            /// # Panics
            /// Panics if $i \geq W$, where $i$ is `index` and $W$ is `$t::WIDTH`.
            ///
            /// # Examples
            /// See [here](super::bit_access#set_bit).
            #[inline]
            fn set_bit(&mut self, index: u64) {
                set_bit_unsigned(self, index)
            }

            /// Sets the $i$th bit of an unsigned primitive integer, or the coefficient of $2^i$ in
            /// its binary expansion, to 0.
            ///
            /// Clearing bits beyond the type's width is allowed; since those bits are already
            /// `false`, clearing them does nothing.
            ///
            /// Let
            /// $$
            /// n = \sum_{i=0}^{W-1} 2^{b_i},
            /// $$
            /// where for all $i$, $b_i\in \\{0, 1\\}$, and $W$ is the width of the type. Then
            /// $$
            /// n \gets \\begin{cases}
            ///     n - 2^j & \text{if} \\quad b_j = 1, \\\\
            ///     n & \text{otherwise}.
            /// \\end{cases}
            /// $$
            ///
            /// # Worst-case complexity
            /// Constant time and additional memory.
            ///
            /// # Examples
            /// See [here](super::bit_access#clear_bit).
            #[inline]
            fn clear_bit(&mut self, index: u64) {
                clear_bit_unsigned(self, index)
            }
        }
    };
}
apply_to_unsigneds!(impl_bit_access_unsigned);

fn get_bit_signed<T: PrimitiveSigned>(x: &T, index: u64) -> bool {
    if index < T::WIDTH {
        *x & (T::ONE << index) != T::ZERO
    } else {
        *x < T::ZERO
    }
}

fn set_bit_signed<T: PrimitiveSigned>(x: &mut T, index: u64) {
    if index < T::WIDTH {
        *x |= T::ONE << index;
    } else if *x >= T::ZERO {
        panic!(
            "Cannot set bit {} in non-negative value of width {}",
            index,
            T::WIDTH
        );
    }
}

fn clear_bit_signed<T: PrimitiveSigned>(x: &mut T, index: u64) {
    if index < T::WIDTH {
        *x &= !(T::ONE << index);
    } else if *x < T::ZERO {
        panic!(
            "Cannot clear bit {} in negative value of width {}",
            index,
            T::WIDTH
        );
    }
}

macro_rules! impl_bit_access_signed {
    ($t:ident) => {
        impl BitAccess for $t {
            /// Determines whether the $i$th bit of a signed primitive integer is 0 or 1.
            ///
            /// `false` means 0 and `true` means 1. Getting bits beyond the type's width is allowed;
            /// those bits are `true` if the value is negative, and `false` otherwise.
            ///
            /// If $n \geq 0$, let
            /// $$
            /// n = \sum_{i=0}^\infty 2^{b_i},
            /// $$
            /// where for all $i$, $b_i\in \\{0, 1\\}$; so finitely many of the bits are 1, and the
            /// rest are 0. Then $f(n, i) = (b_i = 1)$.
            ///
            /// If $n < 0$, let
            /// $$
            /// 2^W + n = \sum_{i=0}^{W-1} 2^{b_i},
            /// $$
            /// where
            /// - $W$ is the type's width
            /// - for all $i$, $b_i\in \\{0, 1\\}$, and $b_i = 1$ for $i \geq W$.
            ///
            /// Then $f(n, j) = (b_j = 1)$.
            ///
            /// # Worst-case complexity
            /// Constant time and additional memory.
            ///
            /// # Examples
            /// See [here](super::bit_access#get_bit).
            #[inline]
            fn get_bit(&self, index: u64) -> bool {
                get_bit_signed(self, index)
            }

            /// Sets the $i$th bit of a signed primitive integer to 1.
            ///
            /// Setting bits beyond the type's width is disallowed if the number is non-negative.
            ///
            /// If $n \geq 0$ and $j \neq W - 1$, let
            /// $$
            /// n = \sum_{i=0}^{W-1} 2^{b_i};
            /// $$
            /// but if $n < 0$ or $j = W - 1$, let
            /// $$
            /// 2^W + n = \sum_{i=0}^{W-1} 2^{b_i},
            /// $$
            /// where for all $i$, $b_i\in \\{0, 1\\}$, and $W$ is the width of the type. Then
            /// $$
            /// n \gets \\begin{cases}
            ///     n + 2^j & \text{if} \\quad b_j = 0, \\\\
            ///     n & \text{otherwise},
            /// \\end{cases}
            /// $$
            /// where $n < 0$ or $j < W$.
            ///
            /// # Worst-case complexity
            /// Constant time and additional memory.
            ///
            /// # Panics
            /// Panics if $n \geq 0$ and $i \geq W$, where $n$ is `self`, $i$ is `index` and $W$ is
            /// the width of the type.
            ///
            /// # Examples
            /// See [here](super::bit_access#set_bit).
            #[inline]
            fn set_bit(&mut self, index: u64) {
                set_bit_signed(self, index)
            }

            /// Sets the $i$th bit of a signed primitive integer to 0.
            ///
            /// Clearing bits beyond the type's width is disallowed if the number is negative.
            ///
            /// If $n \geq 0$ or $j = W - 1$, let
            /// $$
            /// n = \sum_{i=0}^{W-1} 2^{b_i};
            /// $$
            /// but if $n < 0$ or $j = W - 1$, let
            /// $$
            /// 2^W + n = \sum_{i=0}^{W-1} 2^{b_i},
            /// $$
            /// where for all $i$, $b_i\in \\{0, 1\\}$ and $W$ is the width of the type. Then
            /// $$
            /// n \gets \\begin{cases}
            ///     n - 2^j & \text{if} \\quad b_j = 1, \\\\
            ///     n & \text{otherwise},
            /// \\end{cases}
            /// $$
            /// where $n \geq 0$ or $j < W$.
            ///
            /// # Worst-case complexity
            /// Constant time and additional memory.
            ///
            /// # Panics
            /// Panics if $n < 0$ and $i \geq W$, where $n$ is `self`, $i$ is `index` and $W$ is
            /// the width of the type.
            ///
            /// # Examples
            /// See [here](super::bit_access#clear_bit).
            #[inline]
            fn clear_bit(&mut self, index: u64) {
                clear_bit_signed(self, index)
            }
        }
    };
}
apply_to_signeds!(impl_bit_access_signed);