1
- //! This module provides basic bit manipulation operations.
2
- //!
3
- //! The following functions are available:
4
- //!
5
- //! - `get_bit`: Gets the value of a specific bit in a number.
6
- //! - `set_bit`: Sets a specific bit in a number to 1.
7
- //! - `clear_bit`: Sets a specific bit in a number to 0.
8
- //! - `update_bit`: Updates a specific bit in a number based on a boolean value.
9
- //! - `is_even`: Checks if a number is even.
10
- //! - `is_positive`: Checks if a number is positive.
11
- //! - `multiply_by_two`: Multiplies a number by two.
12
- //! - `divide_by_two`: Divides a number by two.
13
- //! - `twos_complement`: Calculates the two's complement of a number.
14
- //! - `multiply_signed`: Multiplies two signed numbers.
15
- //! - `multiply_unsigned`: Multiplies two unsigned numbers.
16
- //! - `count_ones`: Counts the number of ones in a number.
17
- //! - `bit_equivalence`: Counts the number of equal bits between two numbers.
18
- //! - `bit_distance`: Calculates the bit distance between two numbers.
19
- //! - `is_power_of_two`: Checks if a number is a power of two.
20
- //! - `is_power_of_two_difference`: Checks if the number is the difference of two powers of two.
21
- //! - `rightmost_one`: Returns the position of the rightmost one-bit in a number.
22
- //! - `rightmost_zero`: Returns the position of the rightmost zero-bit in a number.
23
- //!
24
- //! For more information on each function, please refer to their individual documentation.
25
-
26
1
/// Gets specific bits from a number.
27
2
///
28
3
/// Returns the value of the bit at position `n` in `bits`.
31
6
///
32
7
/// # Arguments
33
8
///
34
- /// `bits` - The number to extract the bit from.
35
- /// `n` - The position of the bit to extract.
9
+ /// * `bits` - The number to extract the bit from.
10
+ /// * `n` - The position of the bit to extract.
36
11
///
37
12
/// # Returns
38
13
///
39
14
/// The value of the bit at position `n` in `bits`.
40
15
///
41
- /// # Panic
42
- ///
43
- /// This function will not panic.
44
- ///
45
16
/// # Examples
46
17
///
47
18
/// ```rust
@@ -65,17 +36,13 @@ pub fn get_bit(bits: i8, n: usize) -> i8 {
65
36
///
66
37
/// # Arguments
67
38
///
68
- /// `bits` - The number to set the bit in.
69
- /// `n` - The position of the bit to set.
39
+ /// * `bits` - The number to set the bit in.
40
+ /// * `n` - The position of the bit to set.
70
41
///
71
42
/// # Returns
72
43
///
73
44
/// The number with the bit at position `n` set to 1.
74
45
///
75
- /// # Panic
76
- ///
77
- /// This function will not panic.
78
- ///
79
46
/// # Examples
80
47
///
81
48
/// ```rust
@@ -99,17 +66,13 @@ pub fn set_bit(bits: i8, n: usize) -> i8 {
99
66
///
100
67
/// # Arguments
101
68
///
102
- /// `bits` - The number to clear the bit in.
103
- /// `n` - The position of the bit to clear.
69
+ /// * `bits` - The number to clear the bit in.
70
+ /// * `n` - The position of the bit to clear.
104
71
///
105
72
/// # Returns
106
73
///
107
74
/// The number with the bit at position `n` set to 0.
108
75
///
109
- /// # Panic
110
- ///
111
- /// This function will not panic.
112
- ///
113
76
/// # Examples
114
77
///
115
78
/// ```rust
@@ -132,18 +95,14 @@ pub fn clear_bit(bits: i8, n: usize) -> i8 {
132
95
///
133
96
/// # Arguments
134
97
///
135
- /// `bits` - The number to update the bit in.
136
- /// `n` - The position of the bit to update.
137
- /// `set_it` - If true, the bit will be set to 1, otherwise it will be set to 0.
98
+ /// * `bits` - The number to update the bit in.
99
+ /// * `n` - The position of the bit to update.
100
+ /// * `set_it` - If true, the bit will be set to 1, otherwise it will be set to 0.
138
101
///
139
102
/// # Returns
140
103
///
141
104
/// The number with the bit at position `n` set to 1 if `set_it` is true, otherwise set to 0.
142
105
///
143
- /// # Panic
144
- ///
145
- /// This function will not panic.
146
- ///
147
106
/// # Examples
148
107
///
149
108
/// ```rust
@@ -183,16 +142,12 @@ pub fn update_bit(bits: i8, n: usize, set_it: bool) -> i8 {
183
142
///
184
143
/// # Arguments
185
144
///
186
- /// `bits` - The number to check.
145
+ /// * `bits` - The number to check.
187
146
///
188
147
/// # Returns
189
148
///
190
149
/// True if the least significant bit of `bits` is 0, otherwise false.
191
150
///
192
- /// # Panic
193
- ///
194
- /// This function will not panic.
195
- ///
196
151
/// # Examples
197
152
///
198
153
/// ```rust
@@ -220,16 +175,12 @@ pub fn is_even(bits: i8) -> bool {
220
175
///
221
176
/// # Arguments
222
177
///
223
- /// `bits` - The number to check.
178
+ /// * `bits` - The number to check.
224
179
///
225
180
/// # Returns
226
181
///
227
182
/// True if the most significant bit of `bits` is 0, otherwise false.
228
183
///
229
- /// # Panic
230
- ///
231
- /// This function will not panic.
232
- ///
233
184
/// # Examples
234
185
///
235
186
/// ```rust
@@ -261,17 +212,13 @@ pub fn is_positive(bits: i8) -> bool {
261
212
///
262
213
/// # Arguments
263
214
///
264
- /// `bits` - The number to multiply.
215
+ /// * `bits` - The number to multiply.
265
216
///
266
217
/// # Returns
267
218
///
268
219
/// The result of shifting the bits of `bits` one position to the left.
269
220
///
270
- /// # Panic
271
- ///
272
- /// This function will not panic.
273
- ///
274
- /// # Examples
221
+ // # Examples
275
222
///
276
223
/// ```rust
277
224
/// use rust_algorithms::bit_manipulation::multiply_by_two;
@@ -293,16 +240,12 @@ pub fn multiply_by_two(bits: i8) -> i8 {
293
240
///
294
241
/// # Arguments
295
242
///
296
- /// `bits` - The number to divide.
243
+ /// * `bits` - The number to divide.
297
244
///
298
245
/// # Returns
299
246
///
300
247
/// The result of shifting the bits of `bits` one position to the right.
301
248
///
302
- /// # Panic
303
- ///
304
- /// This function will not panic.
305
- ///
306
249
/// # Examples
307
250
///
308
251
/// ```rust
@@ -325,16 +268,12 @@ pub fn divide_by_two(bits: i8) -> i8 {
325
268
///
326
269
/// # Arguments
327
270
///
328
- /// `bits` - The number to calculate the two's complement of.
271
+ /// * `bits` - The number to calculate the two's complement of.
329
272
///
330
273
/// # Returns
331
274
///
332
275
/// The two's complement of `bits`.
333
276
///
334
- /// # Panic
335
- ///
336
- /// This function will not panic.
337
- ///
338
277
/// # Examples
339
278
///
340
279
/// ```rust
@@ -359,17 +298,13 @@ pub fn twos_complement(bits: i8) -> i8 {
359
298
///
360
299
/// # Arguments
361
300
///
362
- /// `a` - The first number to multiply.
363
- /// `b` - The second number to multiply.
301
+ /// * `a` - The first number to multiply.
302
+ /// * `b` - The second number to multiply.
364
303
///
365
304
/// # Returns
366
305
///
367
306
/// The result of multiplying `a` by `b`.
368
307
///
369
- /// # Panic
370
- ///
371
- /// This function will not panic.
372
- ///
373
308
/// # Examples
374
309
///
375
310
/// ```rust
@@ -406,17 +341,13 @@ pub fn multiply_signed(a: i8, b: i8) -> i8 {
406
341
///
407
342
/// # Arguments
408
343
///
409
- /// `a` - The first number to multiply.
410
- /// `b` - The second number to multiply.
344
+ /// * `a` - The first number to multiply.
345
+ /// * `b` - The second number to multiply.
411
346
///
412
347
/// # Returns
413
348
///
414
349
/// The result of multiplying `a` by `b`.
415
350
///
416
- /// # Panic
417
- ///
418
- /// This function will not panic.
419
- ///
420
351
/// # Examples
421
352
///
422
353
/// ```rust
@@ -449,16 +380,12 @@ pub fn multiply_unsigned(a: i8, b: i8) -> i8 {
449
380
///
450
381
/// # Arguments
451
382
///
452
- /// `bits` - The number to count the ones in.
383
+ /// * `bits` - The number to count the ones in.
453
384
///
454
385
/// # Returns
455
386
///
456
387
/// The number of ones in `bits`.
457
388
///
458
- /// # Panic
459
- ///
460
- /// This function will not panic.
461
- ///
462
389
/// # Examples
463
390
///
464
391
/// ```rust
@@ -490,17 +417,13 @@ pub fn count_ones(bits: i8) -> i8 {
490
417
///
491
418
/// # Arguments
492
419
///
493
- /// `a` - The first number to compare.
494
- /// `b` - The second number to compare.
420
+ /// * `a` - The first number to compare.
421
+ /// * `b` - The second number to compare.
495
422
///
496
423
/// # Returns
497
424
///
498
425
/// The number of equal bits between `a` and `b`.
499
426
///
500
- /// # Panic
501
- ///
502
- /// This function will not panic.
503
- ///
504
427
/// # Examples
505
428
///
506
429
/// ```rust
@@ -524,17 +447,13 @@ pub fn bit_equivalence(a: i8, b: i8) -> i8 {
524
447
///
525
448
/// # Arguments
526
449
///
527
- /// `a` - The first number to compare.
528
- /// `b` - The second number to compare.
450
+ /// * `a` - The first number to compare.
451
+ /// * `b` - The second number to compare.
529
452
///
530
453
/// # Returns
531
454
///
532
455
/// The number of different bits between `a` and `b`.
533
456
///
534
- /// # Panic
535
- ///
536
- /// This function will not panic.
537
- ///
538
457
/// # Examples
539
458
///
540
459
/// ```rust
@@ -563,16 +482,12 @@ pub fn bit_distance(a: i8, b: i8) -> i8 {
563
482
///
564
483
/// # Arguments
565
484
///
566
- /// `bits` - The number to check.
485
+ /// * `bits` - The number to check.
567
486
///
568
487
/// # Returns
569
488
///
570
489
/// True if `bits` is a power of two, otherwise false.
571
490
///
572
- /// # Panic
573
- ///
574
- /// This function will not panic.
575
- ///
576
491
/// # Examples
577
492
///
578
493
/// ```rust
@@ -617,16 +532,12 @@ pub fn is_power_of_two(bits: i8) -> bool {
617
532
///
618
533
/// # Arguments
619
534
///
620
- /// `bits` - The number to check.
535
+ /// * `bits` - The number to check.
621
536
///
622
537
/// # Returns
623
538
///
624
539
/// True if `bits` is of the form `2^k - 2^j`, where `k > j`, otherwise `false`.
625
540
///
626
- /// # Panic
627
- ///
628
- /// This function will not panic.
629
- ///
630
541
/// # Examples
631
542
///
632
543
/// ```rust
@@ -655,16 +566,12 @@ pub fn is_power_of_two_difference(bits: i8) -> bool {
655
566
///
656
567
/// # Arguments
657
568
///
658
- /// `bits` - The number to check.
569
+ /// * `bits` - The number to check.
659
570
///
660
571
/// # Returns
661
572
///
662
573
/// The position of the rightmost one-bit in `bits`.
663
574
///
664
- /// # Panic
665
- ///
666
- /// This function will not panic.
667
- ///
668
575
/// # Examples
669
576
///
670
577
/// ```rust
@@ -685,16 +592,12 @@ pub fn rightmost_one(bits: i8) -> i8 {
685
592
///
686
593
/// # Arguments
687
594
///
688
- /// `bits` - The number to check.
595
+ /// * `bits` - The number to check.
689
596
///
690
597
/// # Returns
691
598
///
692
599
/// The position of the rightmost zero-bit in `bits`.
693
600
///
694
- /// # Panic
695
- ///
696
- /// This function will not panic.
697
- ///
698
601
/// # Examples
699
602
///
700
603
/// ```rust
0 commit comments