uberbyte 0.6.1

Bit manipulation for dummies
Documentation
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
use std::{
    ops::{
        Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Shl,
        ShlAssign, Shr, ShrAssign,
    },
    usize,
};

use crate::{
    ALL_BIT_MASK, FIFTH_BIT_MASK, FIRST_BIT_MASK, FOURTH_BIT_MASK, NONE_BIT_MASK, SECOND_BIT_MASK,
    SEVENTH_BIT_MASK, SIXTH_BIT_MASK, THIRD_BIT_MASK, ZERO_BIT_MASK,
};

pub mod formatters;
pub mod try_from;

/// Implements a simple wrapper over a __u8__ that allows you simple bit manipulation
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Copy)]
pub struct UberByte {
    value: u8,
}

///
impl UberByte {
    /// Represents the maximal possible value that the _UberByte_ can handle.
    /// All bits are set to 1.
    ///
    /// # Example
    ///
    /// ```rust
    /// use uberbyte::UberByte;
    ///
    /// let max = UberByte::MAX;
    /// ```
    pub const MAX: UberByte = UberByte {
        value: ALL_BIT_MASK,
    };

    /// Represents the minimal possible value that a _UberByte_ can handle.
    ///
    /// # Example
    ///
    /// ```rust
    /// use uberbyte::UberByte;
    ///
    /// let min = UberByte::MIN;
    /// ```
    pub const MIN: UberByte = UberByte {
        value: NONE_BIT_MASK,
    };

    /// Returns a new instance of a UberByte with the bits set
    ///  to 1 according to the bit max given
    ///
    /// # Example
    ///
    /// ```rust
    /// use uberbyte::UberByte;
    ///
    /// let my_byte = UberByte::MIN;
    ///
    /// let new_byte = my_byte.set(0b_1000_1000);
    /// ```
    pub fn set(&self, bit_mask: u8) -> UberByte {
        let masked_value = (self.value ^ bit_mask) | self.value;

        return UberByte::from(masked_value);
    }

    /// Sets the bits to 1 according to the bit mask
    ///
    /// # Example
    ///
    /// ```rust
    /// use uberbyte::UberByte;
    ///
    /// let mut my_byte = UberByte::MIN;
    ///
    /// my_byte.set_mut(0b_1000_1000)
    /// ```
    pub fn set_mut(&mut self, bit_mask: u8) {
        self.value = (self.value ^ bit_mask) | self.value
    }

    /// Returns a new instance of a UberByte with the bits cleared
    /// according to the given bit mask
    ///
    /// # Example
    ///
    /// ```rust
    /// use uberbyte::UberByte;
    ///
    /// let my_byte = UberByte::MAX;
    ///
    /// let cleared_byte = my_byte.clear(0b_1000_1000);
    /// ```
    pub fn clear(&self, bit_mask: u8) -> UberByte {
        let masked_value = (self.value ^ bit_mask) & self.value;

        return UberByte::from(masked_value);
    }

    /// Clears the bits to 0 according to the given bit mask
    ///
    /// # Example
    ///
    /// ```rust
    /// use uberbyte::UberByte;
    ///
    /// let mut my_byte = UberByte::MIN;
    ///
    /// my_byte.clear_mut(0b_1000_1000)
    /// ```
    pub fn clear_mut(&mut self, bit_mask: u8) {
        self.value = (self.value ^ bit_mask) & self.value
    }

    /// Returns a new instance of a UberByte with all bits flipped
    ///
    /// # Explanation
    ///
    /// Flipping a bit will transform a bit to 0 if 1 and 1 if 0.
    ///
    /// 0 => 1
    /// 1 => 0
    ///
    /// # Example
    ///
    /// ```rust
    /// use uberbyte::UberByte;
    ///
    /// let my_byte = UberByte::MAX;
    ///
    /// let flipped_byte = my_byte.flip();
    /// ```
    pub fn flip(&self) -> UberByte {
        return UberByte::from(!self.value);
    }

    /// Flips all bits in the UberByte
    ///
    /// # Explanation
    ///
    /// Flipping a bit will transform a bit to 0 if 1 and 1 if 0.
    ///
    /// 0 => 1
    /// 1 => 0
    ///
    /// # Example
    ///
    /// ```rust
    /// use uberbyte::UberByte;
    ///
    /// let mut my_byte = UberByte::MAX;
    ///
    /// my_byte.flip_mut();
    /// ```
    pub fn flip_mut(&mut self) {
        self.value = !self.value;
    }

    /// Determines if all bits in the bit mask are also set
    ///
    /// # Returns
    ///
    /// True if all set bits in the bit mask are also set in the UberByte
    /// False if not
    ///
    /// # Example
    ///
    /// ```rust
    /// use uberbyte::UberByte;
    ///
    /// let my_byte = UberByte::from(0b_0101_1010);
    ///
    /// let are_set = my_byte.are_set(0b_0100_1000);
    ///
    /// if (are_set) {
    ///     println!("All bits set");
    /// }
    /// else {
    ///     println!("Not all bits set");
    /// }
    /// ```
    pub fn are_set(&self, bit_mask: u8) -> bool {
        self.value & bit_mask != 0
    }

    /// Determines if the given bit index is set
    ///
    /// # Returns
    ///
    /// TRUE if the index is set
    /// FALSE if the index is not set
    ///
    /// ## Remarks
    ///
    /// If the index given exceeds 7 then the answer is automatically FALSE
    ///
    /// # Example
    ///
    /// ```rust
    /// use uberbyte::UberByte;
    ///
    /// let my_byte = UberByte::from(42);
    ///
    /// for index in 0..7 {
    ///     println!("Bit on index {} is {}", index, my_byte.is_bit_set(index))
    /// }
    /// ```
    pub fn is_bit_set(&self, bit_index: usize) -> bool {
        match bit_index {
            0 => self.is_bit_0_set(),
            1 => self.is_bit_1_set(),
            2 => self.is_bit_2_set(),
            3 => self.is_bit_3_set(),
            4 => self.is_bit_4_set(),
            5 => self.is_bit_5_set(),
            6 => self.is_bit_6_set(),
            7 => self.is_bit_7_set(),
            _ => false,
        }
    }

    /// Determines if the bit at index 0 is set
    ///
    /// # Returns
    ///
    /// given 0000 0001 -> true
    /// given 0000 0000 -> false
    ///
    /// # Remarks
    ///
    /// Ignores the state of all other bits
    pub fn is_bit_0_set(&self) -> bool {
        self.are_set(ZERO_BIT_MASK)
    }

    /// Determines if the bit at index 1 is set
    ///
    /// # Returns
    ///
    /// given 0000 0010 -> true
    /// given 0000 0000 -> false
    ///
    /// # Remarks
    ///
    /// Ignores the state of all other bits
    pub fn is_bit_1_set(&self) -> bool {
        self.are_set(FIRST_BIT_MASK)
    }

    /// Determines if the bit at index 2 is set
    ///
    /// # Returns
    ///
    /// given 0000 0100 -> true
    /// given 0000 0000 -> false
    ///
    /// # Remarks
    ///
    /// Ignores the state of all other bits
    pub fn is_bit_2_set(&self) -> bool {
        self.are_set(SECOND_BIT_MASK)
    }

    /// Determines if the bit at index 3 is set
    ///
    /// # Returns
    ///
    /// given 0000 1000 -> true
    /// given 0000 0000 -> false
    ///
    /// # Remarks
    ///
    /// Ignores the state of all other bits
    pub fn is_bit_3_set(&self) -> bool {
        self.are_set(THIRD_BIT_MASK)
    }

    /// Determines if the bit at index 4 is set
    ///
    /// # Returns
    ///
    /// given 0001 0000 -> true
    /// given 0000 0000 -> false
    ///
    /// # Remarks
    ///
    /// Ignores the state of all other bits
    pub fn is_bit_4_set(&self) -> bool {
        self.are_set(FOURTH_BIT_MASK)
    }

    /// Determines if the bit at index 5 is set
    ///
    /// # Returns
    ///
    /// given 0010 0000 -> true
    /// given 0000 0000 -> false
    ///
    /// # Remarks
    ///
    /// Ignores the state of all other bits
    pub fn is_bit_5_set(&self) -> bool {
        self.are_set(FIFTH_BIT_MASK)
    }

    /// Determines if the bit at index 6 is set
    ///
    /// # Returns
    ///
    /// given 0100 0000 -> true
    /// given 0000 0000 -> false
    ///
    /// # Remarks
    ///
    /// Ignores the state of all other bits
    pub fn is_bit_6_set(&self) -> bool {
        self.are_set(SIXTH_BIT_MASK)
    }

    /// Determines if the bit at index 7 is set
    ///
    /// # Returns
    ///
    /// given 1000 0000 -> true
    /// given 0000 0000 -> false
    ///
    /// # Remarks
    ///
    /// Ignores the state of all other bits
    pub fn is_bit_7_set(&self) -> bool {
        self.are_set(SEVENTH_BIT_MASK)
    }
}

impl Default for UberByte {
    fn default() -> Self {
        Self {
            value: Default::default(),
        }
    }
}

impl AddAssign for UberByte {
    fn add_assign(&mut self, rhs: Self) {
        let sum = self.clone() + rhs;
        self.value = sum.value;
    }
}

impl Add for UberByte {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        let sum: Option<u8> = self.value.checked_add(rhs.value);
        match sum {
            Some(s) => UberByte::from(s),
            None => UberByte::from(ALL_BIT_MASK),
        }
    }
}

impl BitOr for UberByte {
    type Output = Self;

    fn bitor(self, rhs: Self) -> Self::Output {
        UberByte::from(self.value | rhs.value)
    }
}

impl BitOrAssign for UberByte {
    fn bitor_assign(&mut self, rhs: Self) {
        self.value = self.value | rhs.value;
    }
}

impl BitAnd for UberByte {
    type Output = Self;

    fn bitand(self, rhs: Self) -> Self::Output {
        UberByte::from(self.value & rhs.value)
    }
}

impl BitAndAssign for UberByte {
    fn bitand_assign(&mut self, rhs: Self) {
        self.value = self.value & rhs.value;
    }
}

impl BitXor for UberByte {
    type Output = Self;

    fn bitxor(self, rhs: Self) -> Self::Output {
        UberByte::from(self.value ^ rhs.value)
    }
}

impl BitXorAssign for UberByte {
    fn bitxor_assign(&mut self, rhs: Self) {
        self.value = self.value ^ rhs.value;
    }
}

impl Shl for UberByte {
    type Output = Self;

    fn shl(self, rhs: Self) -> Self::Output {
        let shift_left = self.value << rhs.value;
        UberByte::from(shift_left)
    }
}

impl ShlAssign for UberByte {
    fn shl_assign(&mut self, rhs: Self) {
        self.value = self.value << rhs.value;
    }
}

impl Shr for UberByte {
    type Output = Self;

    fn shr(self, rhs: Self) -> Self::Output {
        let shift_right = self.value >> rhs.value;
        UberByte::from(shift_right)
    }
}

impl ShrAssign for UberByte {
    fn shr_assign(&mut self, rhs: Self) {
        self.value = self.value >> rhs.value;
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn individual_bits_set() {
        let test_object = UberByte::from(FIFTH_BIT_MASK);

        assert_eq!(test_object.is_bit_0_set(), false);
        assert_eq!(test_object.is_bit_1_set(), false);
        assert_eq!(test_object.is_bit_2_set(), false);
        assert_eq!(test_object.is_bit_3_set(), false);
        assert_eq!(test_object.is_bit_4_set(), false);
        assert_eq!(test_object.is_bit_5_set(), true);
    }

    #[test]
    fn is_bit_set() {
        assert!(UberByte::MAX.is_bit_set(0));
        assert!(UberByte::MAX.is_bit_set(1));
        assert!(UberByte::MAX.is_bit_set(2));
        assert!(UberByte::MAX.is_bit_set(3));
        assert!(UberByte::MAX.is_bit_set(4));
        assert!(UberByte::MAX.is_bit_set(5));
        assert!(UberByte::MAX.is_bit_set(6));
        assert!(UberByte::MAX.is_bit_set(7));
        assert_eq!(false, UberByte::MAX.is_bit_set(8));

        assert_eq!(false, UberByte::MIN.is_bit_set(0));
        assert_eq!(false, UberByte::MIN.is_bit_set(1));
        assert_eq!(false, UberByte::MIN.is_bit_set(2));
        assert_eq!(false, UberByte::MIN.is_bit_set(3));
        assert_eq!(false, UberByte::MIN.is_bit_set(4));
        assert_eq!(false, UberByte::MIN.is_bit_set(5));
        assert_eq!(false, UberByte::MIN.is_bit_set(6));
        assert_eq!(false, UberByte::MIN.is_bit_set(7));

        assert_eq!(false, UberByte::from(42).is_bit_set(0));
        assert_eq!(true, UberByte::from(42).is_bit_set(1));
        assert_eq!(false, UberByte::from(42).is_bit_set(2));
        assert_eq!(true, UberByte::from(42).is_bit_set(3));
        assert_eq!(false, UberByte::from(42).is_bit_set(4));
        assert_eq!(true, UberByte::from(42).is_bit_set(5));
        assert_eq!(false, UberByte::from(42).is_bit_set(6));
        assert_eq!(false, UberByte::from(42).is_bit_set(7));
    }

    #[test]
    fn set_per_mask() {
        let test_object = UberByte::MIN;
        let test_result = test_object.set(0b_1001_0000);

        assert_eq!(test_result.value, 0b_1001_0000);
    }

    #[test]
    fn set_per_mask_collision() {
        let test_object = UberByte::from(0b_1000_1000);
        let test_result = test_object.set(0b_1001_0000);

        assert_eq!(test_result.value, 0b_1001_1000);
    }

    #[test]
    fn clear_per_mask() {
        let test_object = UberByte::MAX;
        let test_result = test_object.clear(0b_1001_0000);

        assert_eq!(test_result.value, 0b_0110_1111);
    }

    #[test]
    fn flip() {
        assert_eq!(UberByte::MAX.flip(), UberByte::MIN);
        assert_eq!(UberByte::MIN.flip(), UberByte::MAX);
        assert_eq!(
            UberByte::from(0b_0101_0101).flip(),
            UberByte::from(0b_1010_1010)
        );
    }

    #[test]
    fn clear_per_mask_collision() {
        let test_object = UberByte::from(0b_1010_0010);
        let test_result = test_object.clear(0b_1001_0000);

        assert_eq!(test_result.value, 0b_0010_0010);
    }

    #[test]
    fn are_set() {
        let test_object = UberByte::from(0b_0001_1000);

        assert!(test_object.are_set(THIRD_BIT_MASK));
        assert!(test_object.are_set(FOURTH_BIT_MASK));
        assert!(test_object.are_set(THIRD_BIT_MASK | FOURTH_BIT_MASK));
    }

    #[test]
    fn add_overflow() {
        let augend = UberByte::MAX;
        let added = UberByte::MAX;

        assert_eq!(UberByte::MAX, augend + added)
    }

    #[test]
    fn add() {
        let augend = UberByte::from(5);
        let added = UberByte::from(6);

        assert_eq!(UberByte::from(11), augend + added);
    }

    #[test]
    fn or() {
        let first = UberByte::from(0b_0001_0001);
        let second = UberByte::from(0b_0000_0001);

        let test_result = first | second;

        assert_eq!(UberByte::from(0b_0001_0001), test_result)
    }

    #[test]
    fn or_assign() {
        let mut test_result = UberByte::from(0b_0001_0001);
        let second = UberByte::from(0b_0000_0001);

        test_result |= second;

        assert_eq!(UberByte::from(0b_0001_0001), test_result)
    }

    #[test]
    fn xor() {
        let first = UberByte::from(0b_0001_0001);
        let second = UberByte::from(0b_0000_0001);

        let test_result = first ^ second;

        assert_eq!(UberByte::from(0b_0001_0000), test_result)
    }

    #[test]
    fn xor_assign() {
        let mut test_result = UberByte::from(0b_0001_0001);
        let second = UberByte::from(0b_0000_0001);

        test_result ^= second;

        assert_eq!(UberByte::from(0b_0001_0000), test_result)
    }

    #[test]
    fn and() {
        let first = UberByte::from(0b_0001_0001);
        let second = UberByte::from(0b_0000_0001);

        let test_result = first & second;

        assert_eq!(UberByte::from(0b_0000_0001), test_result)
    }

    #[test]
    fn and_assign() {
        let mut test_result = UberByte::from(0b_0001_0001);
        let second = UberByte::from(0b_0000_0001);

        test_result &= second;

        assert_eq!(UberByte::from(0b_0000_0001), test_result)
    }

    #[test]
    fn shl() {
        let test_object = UberByte::from(0b_0000_0001);
        let shift = UberByte::from(1);

        let test_result = test_object << shift;

        assert_eq!(UberByte::from(0b_0000_0010), test_result);
    }

    #[test]
    fn shl_assign() {
        let mut test_object = UberByte::from(0b_0000_0001);
        let shift = UberByte::from(1);

        test_object <<= shift;

        assert_eq!(UberByte::from(0b_0000_0010), test_object);
    }

    #[test]
    fn shr() {
        let test_object = UberByte::from(0b_0000_1000);
        let shift = UberByte::from(1);

        let test_result = test_object >> shift;

        assert_eq!(UberByte::from(0b_0000_0100), test_result);
    }

    #[test]
    fn shr_assign() {
        let mut test_object = UberByte::from(0b_0000_1000);
        let shift = UberByte::from(1);

        test_object >>= shift;

        assert_eq!(UberByte::from(0b_0000_0100), test_object);
    }
}