brainfoamkit_lib/byte.rs
1// SPDX-FileCopyrightText: 2023 - 2024 Ali Sajid Imami
2//
3// SPDX-License-Identifier: Apache-2.0
4// SPDX-License-Identifier: MIT
5
6use std::{
7 fmt::{
8 self,
9 Display,
10 Formatter,
11 },
12 ops::{
13 BitAnd,
14 BitAndAssign,
15 BitOr,
16 BitOrAssign,
17 BitXor,
18 BitXorAssign,
19 Not,
20 },
21};
22
23use crate::{
24 Bit,
25 IterableByte,
26 Nybble,
27};
28
29/// A Byte is an 8-bit unsigned integer (u8).
30///
31/// This is a wrapper around eight Bit instances. The least significant bit is
32/// `bit_0` and the most significant bit is `bit_7`. This struct is used to
33/// conveniently manipulate 8-bit values.
34///
35/// Note that the Bit instances are stored in reverse (LSB to MSB) order,
36/// but the constructor takes them in the correct order (MSB to LSB) to provide
37/// a predictable and intuitive interface.
38///
39/// # Examples
40///
41/// ## Create a byte from primitive Bit values
42///
43/// An easy way create a byte is to use the [`Byte::new()`](#method.new) method.
44/// This method takes eight [Bit](crate::Bit) instances as arguments. The least
45/// significant bit is `bit_0` and the most significant bit is `bit_7`.
46/// The order of the arguments is the same as the order of the bits in the byte.
47///
48/// ```
49/// use brainfoamkit_lib::{
50/// Bit,
51/// Byte,
52/// };
53///
54/// let byte = Byte::new(
55/// Bit::one(),
56/// Bit::zero(),
57/// Bit::one(),
58/// Bit::zero(),
59/// Bit::one(),
60/// Bit::zero(),
61/// Bit::one(),
62/// Bit::zero(),
63/// );
64/// assert_eq!(u8::from(&byte), 0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
65/// assert_eq!(byte.to_string(), "0xAA");
66/// ```
67/// ## Create a byte from a primitive u8 value
68///
69/// ```
70/// use brainfoamkit_lib::Byte;
71///
72/// let byte = Byte::from(0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
73/// assert_eq!(u8::from(&byte), 0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
74/// assert_eq!(byte.to_string(), "0xAA");
75/// ```
76///
77/// ## Create a byte from two Nybbles
78///
79/// ```
80/// use brainfoamkit_lib::{
81/// Byte,
82/// Nybble,
83/// };
84///
85/// let high_nybble = Nybble::from(0b1011); // Dec: 11; Hex: 0x0B; Oct: 0o13
86/// let low_nybble = Nybble::from(0b0101); // Dec: 5; Hex: 0x05; Oct: 0o5
87/// let byte = Byte::from_nybbles(high_nybble, low_nybble);
88/// assert_eq!(u8::from(&byte), 0b10110101); // Dec: 181; Hex: 0xB5; Oct: 0o265
89/// assert_eq!(byte.to_string(), "0xB5");
90/// ```
91///
92/// ## Set and Unset bits to generate desired byte
93///
94/// ```
95/// use brainfoamkit_lib::Byte;
96/// use brainfoamkit_lib::Bit;
97///
98/// let mut byte = Byte::default(); // Byte: 0b00000000; Dec: 0; Hex: 0x00; Oct: 0o0
99/// byte.set_bit(0); // Byte: 0b00000001; Dec: 1; Hex: 0x01; Oct: 0o1
100/// byte.set_bit(1); // Byte: 0b00000011; Dec: 3; Hex: 0x03; Oct: 0o3
101/// byte.set_bit(2); // Byte: 0b00000111; Dec: 7; Hex: 0x07; Oct: 0o7
102/// assert_eq!(u8::from(&byte), 0b00000111); // Dec: 7; Hex: 0x07; Oct: 0o7
103/// assert_eq!(byte.to_string(), "0x07");
104/// byte.unset_bit(1); // Byte: 0b00000101; Dec: 5; Hex: 0x05; Oct: 0o5
105/// assert_eq!(u8::from(&byte), 0b00000101); // Dec: 5; Hex: 0x05; Oct: 0o5
106/// assert_eq!(byte.to_string(), "0x05");
107/// ```
108///
109/// ## Get the Bit value at a given index
110///
111/// ```
112/// use brainfoamkit_lib::{
113/// Bit,
114/// Byte,
115/// };
116///
117/// let byte = Byte::from(0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
118/// assert_eq!(byte.get_bit(0), Bit::Zero); // Least significant bit
119/// assert_eq!(byte.get_bit(1), Bit::One);
120/// assert_eq!(byte.get_bit(2), Bit::Zero);
121/// assert_eq!(byte.get_bit(3), Bit::One);
122/// assert_eq!(byte.get_bit(4), Bit::Zero);
123/// assert_eq!(byte.get_bit(5), Bit::One);
124/// assert_eq!(byte.get_bit(6), Bit::Zero);
125/// assert_eq!(byte.get_bit(7), Bit::One); // Most significant bit
126/// ```
127///
128/// ## Flip the Bit value at a given index
129///
130/// ```
131/// use brainfoamkit_lib::Byte;
132/// use brainfoamkit_lib::Bit;
133///
134/// let mut byte = Byte::default(); // Byte: 0b00000000; Dec: 0; Hex: 0x00; Oct: 0o0
135/// byte.set_bit(0); // Byte: 0b00000001; Dec: 1; Hex: 0x01; Oct: 0o1
136/// byte.set_bit(2); // Byte: 0b00000101; Dec: 5; Hex: 0x05; Oct: 0o5
137/// byte.set_bit(4); // Byte: 0b00010101; Dec: 21; Hex: 0x15; Oct: 0o25
138/// assert_eq!(u8::from(&byte), 0b00010101); // Dec: 21; Hex: 0x15; Oct: 0o25
139/// assert_eq!(byte.to_string(), "0x15");
140/// byte.flip_bit(2); // Byte: 0b00010001; Dec: 17; Hex: 0x11; Oct: 0o21
141/// assert_eq!(u8::from(&byte), 0b00010001); // Dec: 17; Hex: 0x11; Oct: 0o21
142/// assert_eq!(byte.to_string(), "0x11");
143/// byte.flip_bit(7); // Byte: 0b10010001; Dec: 145; Hex: 0x91; Oct: 0o221
144/// assert_eq!(u8::from(&byte), 0b10010001); // Dec: 145; Hex: 0x91; Oct: 0o221
145/// assert_eq!(byte.to_string(), "0x91");
146/// ```
147///
148/// # Panics
149///
150/// The methods [`set_bit()`](#method.set_bit),
151/// [`unset_bit()`](#method.unset_bit) and [`get_bit()`](#method.get_bit) will
152/// panic if the index is out of bounds.
153///
154/// # See Also
155///
156/// * [`Bit`](crate::Bit): A single bit.
157/// * [`Nybble`](crate::Nybble): A 4-bit unsigned integer (u4).
158#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
159pub struct Byte {
160 bit_0: Bit,
161 bit_1: Bit,
162 bit_2: Bit,
163 bit_3: Bit,
164 bit_4: Bit,
165 bit_5: Bit,
166 bit_6: Bit,
167 bit_7: Bit,
168}
169
170impl Byte {
171 /// Creates a new Byte instance with the specified Bit values.
172 ///
173 /// This method takes eight Bit instances as arguments. The least
174 /// significant bit is `bit_0` and the most significant bit is `bit_7`.
175 ///
176 /// Note that the Bit instances are stored in reverse (LSB to MSB) order,
177 /// but the constructor takes them in the correct order (MSB to LSB) to
178 /// provide a predictable and intuitive interface.
179 ///
180 /// # Arguments
181 ///
182 /// * `zeroth` - The value of the most significant bit.
183 /// * `first` - The value of the second most significant bit.
184 /// * `second` - The value of the third most significant bit.
185 /// * `third` - The value of the fourth most significant bit.
186 /// * `fourth` - The value of the fifth most significant bit.
187 /// * `fifth` - The value of the sixth most significant bit.
188 /// * `sixth` - The value of the seventh most significant bit.
189 /// * `seventh` - The value of the least significant bit.
190 ///
191 /// # Examples
192 ///
193 /// ## Single Digit
194 /// ```
195 /// use brainfoamkit_lib::Byte;
196 /// use brainfoamkit_lib::Bit;
197 ///
198 /// let byte_single = Byte::new(Bit::zero(), Bit::zero(), Bit::zero(), Bit::zero(), Bit::one(), Bit::zero(), Bit::one(), Bit::zero());
199 /// assert_eq!(u8::from(&byte_single), 0b00001010); // Dec: 10; Hex: 0x0A; Oct: 0o12
200 /// assert_eq!(byte_single.to_string(), "0x0A");
201 /// ```
202 /// ## Double Digit
203 /// ```
204 /// use brainfoamkit_lib::Byte;
205 /// use brainfoamkit_lib::Bit;
206 ///
207 /// let byte_double = Byte::new(Bit::one(), Bit::zero(), Bit::one(), Bit::zero(), Bit::one(), Bit::zero(), Bit::one(), Bit::zero());
208 /// assert_eq!(u8::from(&byte_double), 0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
209 /// assert_eq!(byte_double.to_string(), "0xAA");
210 /// ```
211 ///
212 /// # Returns
213 ///
214 /// A Byte containing the specified Bit values.
215 ///
216 /// # See Also
217 ///
218 /// * [`from_u8()`](#method.from_u8): Create a new Byte from a u8.
219 /// * [`from_nybbles()`](#method.from_nybbles): Create a new Byte from two
220 /// [Nybbles](crate::Nybble).
221 /// * [`default()`](#method.default): Create a new Byte with all bits set to
222 /// zero.
223 #[must_use]
224 #[allow(clippy::too_many_arguments)]
225 pub const fn new(
226 zeroth: Bit,
227 first: Bit,
228 second: Bit,
229 third: Bit,
230 fourth: Bit,
231 fifth: Bit,
232 sixth: Bit,
233 seventh: Bit,
234 ) -> Self {
235 Self {
236 bit_0: seventh, // Least significant bit
237 bit_1: sixth,
238 bit_2: fifth,
239 bit_3: fourth, // Low Nybble from here on Up
240 bit_4: third, // High Nybble from here on Down
241 bit_5: second,
242 bit_6: first,
243 bit_7: zeroth, // Most Significant Bit
244 }
245 }
246
247 /// Creates a new Byte from two Nybbles.
248 ///
249 /// This method takes two [Nybbles](crate::Nybble) as arguments.
250 /// The first Nybble (`bit_7` to `bit_4`) is the High Nybble and
251 /// the second Nybble (`bit_3` to `bit_0`) is the Low Nybble.
252 ///
253 /// # Arguments
254 ///
255 /// * `high_nybble` - The High Nybble of the Byte.
256 /// * `low_nybble` - The Low Nybble of the Byte.
257 ///
258 /// # Examples
259 ///
260 /// ```
261 /// use brainfoamkit_lib::{
262 /// Byte,
263 /// Nybble,
264 /// };
265 ///
266 /// let high_nybble = Nybble::from(0b1011); // Dec: 11; Hex: 0x0B; Oct: 0o13
267 /// let low_nybble = Nybble::from(0b0101); // Dec: 5; Hex: 0x05; Oct: 0o5
268 /// let byte = Byte::from_nybbles(high_nybble, low_nybble);
269 /// assert_eq!(u8::from(&byte), 0b10110101); // Dec: 181; Hex: 0xB5; Oct: 0o265
270 /// assert_eq!(byte.to_string(), "0xB5");
271 /// ```
272 ///
273 /// # Returns
274 /// A Byte containing the value of the two Nybbles.
275 ///
276 /// # See Also
277 ///
278 /// * [`get_high_nybble()`](#method.get_high_nybble): Get the High Nybble of
279 /// the Byte.
280 /// * [`get_low_nybble()`](#method.get_low_nybble): Get the Low Nybble of
281 /// the Byte.
282 /// * [`from_u8()`](#method.from_u8): Create a new Byte from a u8.
283 /// * [`default()`](#method.default): Create a new Byte with all bits set to
284 /// zero.
285 /// * [`to_u8()`](#method.to_u8): Convert the Byte to a u8.
286 /// * [`new()`](#method.new): Create a new Byte from individual Bit values.
287 #[must_use]
288 pub fn from_nybbles(high_nybble: Nybble, low_nybble: Nybble) -> Self {
289 let mut byte = Self::default();
290
291 for i in 0..4 {
292 if high_nybble.get_bit(i) == Bit::One {
293 byte.set_bit((i + 4) as usize);
294 }
295 if low_nybble.get_bit(i) == Bit::One {
296 byte.set_bit(i as usize);
297 }
298 }
299
300 byte
301 }
302
303 /// Gets the High or First Nybble from the Byte.
304 /// This method returns a [Nybble](crate::Nybble).
305 /// The High Nybble is the first nybble (`bit_7` to `bit_4`).
306 ///
307 /// # Examples
308 ///
309 /// ```
310 /// use brainfoamkit_lib::Byte;
311 ///
312 /// let byte = Byte::from(0b01010101); // Dec: 85; Hex: 0x55; Oct: 0o125
313 /// let high_nybble = byte.get_high_nybble(); // Nybble: 0b0101; Dec: 5; Hex: 0x05; Oct: 0o5
314 /// assert_eq!(u8::from(&high_nybble), 0b0101); // Dec: 5; Hex: 0x05; Oct: 0o5
315 /// assert_eq!(high_nybble.to_string(), "0x5");
316 /// ```
317 ///
318 /// # Returns
319 ///
320 /// A Nybble representing the High Nybble of the Byte.
321 ///
322 /// # See Also
323 ///
324 /// * [`get_low_nybble()`](#method.get_low_nybble): Get the Low Nybble of
325 /// the Byte.
326 /// * [`from_nybbles()`](#method.from_nybbles): Create a new Byte from two
327 /// Nybbles.
328 #[must_use]
329 pub fn get_high_nybble(&self) -> Nybble {
330 let mut nybble = Nybble::default();
331
332 for i in 0..4 {
333 if self.get_bit(i + 4) == Bit::One {
334 nybble.set_bit(i);
335 }
336 }
337
338 nybble
339 }
340
341 /// Gets the Low or Second Nybble from the Byte.
342 /// This method returns a Nybble.
343 /// The Low Nybble is the second nybble (`bit_3` to `bit_0`).
344 ///
345 /// # Examples
346 ///
347 /// ```
348 /// use brainfoamkit_lib::Byte;
349 ///
350 /// let byte = Byte::from(0b01010101); // Byte: 0b01010101; Dec: 85; Hex: 0x55; Oct: 0o125
351 /// let low_nybble = byte.get_low_nybble(); // Nybble: 0b0101; Dec: 5; Hex: 0x05; Oct: 0o5
352 /// assert_eq!(u8::from(&low_nybble), 0b0101); // Dec: 5; Hex: 0x05; Oct: 0o5
353 /// assert_eq!(low_nybble.to_string(), "0x5");
354 /// ```
355 ///
356 /// # Returns
357 ///
358 /// A Nybble containing the least significant nybble of the Byte.
359 ///
360 /// # See Also
361 ///
362 /// * [`get_high_nybble()`](#method.get_high_nybble): Get the High Nybble of
363 /// the Byte.
364 /// * [`from_nybbles()`](#method.from_nybbles): Create a new Byte from two
365 /// Nybbles.
366 #[must_use]
367 pub fn get_low_nybble(&self) -> Nybble {
368 let mut nybble = Nybble::default();
369
370 for i in 0..4 {
371 if self.get_bit(i) == Bit::One {
372 nybble.set_bit(i);
373 }
374 }
375
376 nybble
377 }
378
379 /// Sets the Bit value at the specified index.
380 ///
381 /// This method is used "Set" the bit value at a given index.
382 /// This means that that bit value is set to 1.
383 ///
384 /// The index is zero-based, so the least significant bit is at index 0 and
385 /// the most significant bit is at index 7.
386 ///
387 /// # Arguments
388 ///
389 /// * `index` - The index of the Bit to set.
390 ///
391 /// # Examples
392 ///
393 /// ```
394 /// use brainfoamkit_lib::Byte;
395 ///
396 /// let mut byte = Byte::default(); // Byte: 0b00000000; Dec: 0; Hex: 0x00; Oct: 0o0
397 /// byte.set_bit(0); // Byte: 0b00000001; Dec: 1; Hex: 0x01; Oct: 0o1
398 /// byte.set_bit(2); // Byte: 0b00000101; Dec: 5; Hex: 0x05; Oct: 0o5
399 /// assert_eq!(u8::from(&byte), 0b00000101); // Dec: 5; Hex: 0x05; Oct: 0o5
400 /// assert_eq!(byte.to_string(), "0x05");
401 /// byte.set_bit(4); // Byte: 0b00010101; Dec: 21; Hex: 0x15; Oct: 0o25
402 /// byte.set_bit(6); // Byte: 0b01010101; Dec: 85; Hex: 0x55; Oct: 0o125
403 /// assert_eq!(u8::from(&byte), 0b01010101); // Dec: 85; Hex: 0x55; Oct: 0o125
404 /// assert_eq!(byte.to_string(), "0x55");
405 /// ```
406 ///
407 /// # Panics
408 ///
409 /// This method will panic if the index is out of bounds.
410 ///
411 /// # Side Effects
412 ///
413 /// This method will flip the Bit value at the specified index.
414 ///
415 /// # See Also
416 ///
417 /// * [`unset_bit()`](#method.unset_bit): Unset the Bit value at the
418 /// specified index.
419 /// * [`flip_bit()`](#method.flip_bit): Flip the Bit value at the specified
420 /// index.
421 /// * [`get_bit()`](#method.get_bit): Get the Bit value at the specified
422 /// index.
423 pub fn set_bit(&mut self, index: usize) {
424 match index {
425 0 => self.bit_0.set(),
426 2 => self.bit_2.set(),
427 1 => self.bit_1.set(),
428 3 => self.bit_3.set(),
429 4 => self.bit_4.set(),
430 5 => self.bit_5.set(),
431 6 => self.bit_6.set(),
432 7 => self.bit_7.set(),
433 _ => unreachable!("Index out of bounds"),
434 }
435 }
436
437 /// Unsets the Bit value at the specified index.
438 ///
439 /// This method is used "Unset" the bit value at a given index.
440 /// This means that that bit value is set to 0.
441 ///
442 /// The index is zero-based, so the least significant bit is at index 0 and
443 /// the most significant bit is at index 7.
444 ///
445 /// # Arguments
446 ///
447 /// * `index` - The index of the Bit to unset.
448 ///
449 /// # Examples
450 ///
451 /// ```
452 /// use brainfoamkit_lib::Byte;
453 /// use brainfoamkit_lib::Bit;
454 ///
455 /// let mut byte = Byte::default(); // Byte: 0b00000000; Dec: 0; Hex: 0x00; Oct: 0o0
456 /// byte.set_bit(0); // Byte: 0b00000001; Dec: 1; Hex: 0x01; Oct: 0o1
457 /// byte.set_bit(2); // Byte: 0b00000101; Dec: 5; Hex: 0x05; Oct: 0o5
458 /// assert_eq!(u8::from(&byte), 0b00000101); // Dec: 5; Hex: 0x05; Oct: 0o5
459 /// assert_eq!(byte.to_string(), "0x05");
460 /// byte.unset_bit(0); // Byte: 0b00000100; Dec: 4; Hex: 0x04; Oct: 0o4
461 /// assert_eq!(u8::from(&byte), 0b00000100); // Dec: 4; Hex: 0x04; Oct: 0o4
462 /// assert_eq!(byte.to_string(), "0x04");
463 /// ```
464 ///
465 /// # Panics
466 ///
467 /// This method will panic if the index is out of bounds.
468 ///
469 /// # Side Effects
470 ///
471 /// This method will [unset](crate::Bit#method.unset) the Bit value at the
472 /// specified index.
473 ///
474 /// # See Also
475 ///
476 /// * [`set_bit()`](#method.set_bit): Set the Bit value at the specified
477 /// index.
478 /// * [`flip_bit()`](#method.flip_bit): Flip the Bit value at the specified
479 /// index.
480 /// * [`get_bit()`](#method.get_bit): Get the Bit value at the specified
481 /// index.
482 pub fn unset_bit(&mut self, index: usize) {
483 match index {
484 0 => self.bit_0.unset(),
485 1 => self.bit_1.unset(),
486 2 => self.bit_2.unset(),
487 3 => self.bit_3.unset(),
488 4 => self.bit_4.unset(),
489 5 => self.bit_5.unset(),
490 6 => self.bit_6.unset(),
491 7 => self.bit_7.unset(),
492 _ => unreachable!("Index out of bounds"),
493 }
494 }
495
496 /// Get the Bit value at the specified index.
497 ///
498 /// The index is zero-based, so the least significant bit is at index 0 and
499 /// the most significant bit is at index 7.
500 ///
501 /// # Arguments
502 ///
503 /// * `index` - The index of the Bit to get.
504 ///
505 /// # Examples
506 ///
507 /// ```
508 /// use brainfoamkit_lib::{
509 /// Bit,
510 /// Byte,
511 /// };
512 ///
513 /// let byte = Byte::new(
514 /// Bit::one(),
515 /// Bit::zero(),
516 /// Bit::one(),
517 /// Bit::zero(),
518 /// Bit::one(),
519 /// Bit::zero(),
520 /// Bit::one(),
521 /// Bit::zero(),
522 /// );
523 /// assert_eq!(u8::from(&byte), 0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
524 /// assert_eq!(byte.get_bit(0), Bit::Zero); // Least significant bit
525 /// assert_eq!(byte.get_bit(1), Bit::One);
526 /// assert_eq!(byte.get_bit(2), Bit::Zero);
527 /// assert_eq!(byte.get_bit(3), Bit::One);
528 /// assert_eq!(byte.get_bit(4), Bit::Zero);
529 /// assert_eq!(byte.get_bit(5), Bit::One);
530 /// assert_eq!(byte.get_bit(6), Bit::Zero);
531 /// assert_eq!(byte.get_bit(7), Bit::One); // Most significant bit
532 /// ```
533 ///
534 /// # Panics
535 ///
536 /// This method will panic if the index is out of bounds.
537 ///
538 /// # Returns
539 ///
540 /// A [Bit](crate::Bit) containing the value of the Bit at the specified
541 /// index.
542 ///
543 /// # See Also
544 ///
545 /// * [`set_bit()`](#method.set_bit): Set the Bit value at the specified
546 /// index.
547 /// * [`unset_bit()`](#method.unset_bit): Unset the Bit value at the
548 /// specified index.
549 /// * [`flip_bit()`](#method.flip_bit): Flip the Bit value at the specified
550 /// index.
551 #[must_use]
552 pub fn get_bit(&self, index: u8) -> Bit {
553 match index {
554 0 => self.bit_0,
555 1 => self.bit_1,
556 2 => self.bit_2,
557 3 => self.bit_3,
558 4 => self.bit_4,
559 5 => self.bit_5,
560 6 => self.bit_6,
561 7 => self.bit_7,
562 _ => panic!("Index out of bounds"),
563 }
564 }
565
566 /// Flips the Bit value at the specified index.
567 ///
568 /// This method is used to flip the bit value at a given index.
569 /// This means that that bit value is set to the opposite of its current
570 /// value.
571 ///
572 /// The index is zero-based, so the least significant bit is at index 0 and
573 /// the most significant bit is at index 7.
574 ///
575 /// # Arguments
576 ///
577 /// * `index` - The index of the Bit to flip.
578 ///
579 /// # Examples
580 ///
581 /// ```
582 /// use brainfoamkit_lib::Byte;
583 ///
584 /// let mut byte = Byte::default(); // Byte: 0b00000000; Dec: 0; Hex: 0x00; Oct: 0o0
585 /// byte.set_bit(0); // Byte: 0b00000001; Dec: 1; Hex: 0x01; Oct: 0o1
586 /// byte.set_bit(2); // Byte: 0b00000101; Dec: 5; Hex: 0x05; Oct: 0o5
587 /// byte.set_bit(4); // Byte: 0b00010101; Dec: 21; Hex: 0x15; Oct: 0o25
588 /// byte.set_bit(6); // Byte: 0b01010101; Dec: 85; Hex: 0x55; Oct: 0o125
589 ///
590 /// assert_eq!(u8::from(&byte), 0b01010101); // Dec: 85; Hex: 0x55; Oct: 0o125
591 /// assert_eq!(byte.to_string(), "0x55");
592 ///
593 /// byte.flip_bit(0); // Byte: 0b01010100; Dec: 84; Hex: 0x54; Oct: 0o124
594 /// byte.flip_bit(1); // Byte: 0b01010110; Dec: 86; Hex: 0x56; Oct: 0o126
595 /// byte.flip_bit(2); // Byte: 0b01010010; Dec: 82; Hex: 0x52; Oct: 0o122
596 /// byte.flip_bit(3); // Byte: 0b01011010; Dec: 90; Hex: 0x5A; Oct: 0o132
597 /// byte.flip_bit(4); // Byte: 0b01111010; Dec: 122; Hex: 0x7A; Oct: 0o172
598 /// byte.flip_bit(5); // Byte: 0b00111010; Dec: 58; Hex: 0x3A; Oct: 0o72
599 /// byte.flip_bit(6); // Byte: 0b10111010; Dec: 186; Hex: 0xBA; Oct: 0o272
600 /// byte.flip_bit(7); // Byte: 0b00111010; Dec: 122; Hex: 0x7A; Oct: 0o172
601 ///
602 /// assert_eq!(u8::from(&byte), 0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
603 /// assert_eq!(byte.to_string(), "0xAA");
604 /// ```
605 ///
606 /// # Panics
607 ///
608 /// This method will panic if the index is out of bounds.
609 ///
610 /// # Side Effects
611 ///
612 /// This method will [`flip`](crate::Bit#method.flip) the Bit value at the
613 /// specified index.
614 ///
615 /// # See Also
616 ///
617 /// * [`set_bit()`](#method.set_bit): Set the Bit value at the specified
618 /// index.
619 /// * [`unset_bit()`](#method.unset_bit): Unset the Bit value at the
620 /// specified index.
621 /// * [`get_bit()`](#method.get_bit): Get the Bit value at the specified
622 /// index.
623 pub fn flip_bit(&mut self, index: u8) {
624 match index {
625 0..=7 => {
626 match index {
627 0 => &mut self.bit_0,
628 1 => &mut self.bit_1,
629 2 => &mut self.bit_2,
630 3 => &mut self.bit_3,
631 4 => &mut self.bit_4,
632 5 => &mut self.bit_5,
633 6 => &mut self.bit_6,
634 7 => &mut self.bit_7,
635 _ => unreachable!(),
636 }
637 .flip();
638 }
639 _ => panic!("Index out of bounds"),
640 }
641 }
642
643 /// Flips all of the Bit values in the Byte.
644 ///
645 /// This method is used to flip all of the bit values in the Byte.
646 /// This means that all of the bit values are set to the opposite of their
647 /// current values. This can be used to find the two's complement of a
648 /// Byte.
649 ///
650 /// # Examples
651 ///
652 /// ```
653 /// use brainfoamkit_lib::Byte;
654 ///
655 /// let mut byte = Byte::default(); // Byte: 0b00000000; Dec: 0; Hex: 0x00; Oct: 0o0
656 ///
657 /// byte.set_bit(0); // Byte: 0b00000001; Dec: 1; Hex: 0x01; Oct: 0o1
658 /// byte.set_bit(2); // Byte: 0b00000101; Dec: 5; Hex: 0x05; Oct: 0o5
659 /// byte.set_bit(4); // Byte: 0b00010101; Dec: 21; Hex: 0x15; Oct: 0o25
660 /// byte.set_bit(6); // Byte: 0b01010101; Dec: 85; Hex: 0x55; Oct: 0o125
661 ///
662 /// assert_eq!(u8::from(&byte), 0b01010101); // Dec: 85; Hex: 0x55; Oct: 0o125
663 /// assert_eq!(byte.to_string(), "0x55");
664 ///
665 /// byte.flip();
666 ///
667 /// assert_eq!(u8::from(&byte), 0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
668 /// assert_eq!(byte.to_string(), "0xAA");
669 /// ```
670 ///
671 /// # Side Effects
672 ///
673 /// This method will [`flip`](crate::Bit#method.flip) all of the Bit values
674 /// in the Byte.
675 ///
676 /// # See Also
677 ///
678 /// * [`flip_bit()`](#method.flip_bit): Flip the Bit value at the specified
679 /// index.
680 pub fn flip(&mut self) {
681 self.bit_0.flip();
682 self.bit_1.flip();
683 self.bit_2.flip();
684 self.bit_3.flip();
685 self.bit_4.flip();
686 self.bit_5.flip();
687 self.bit_6.flip();
688 self.bit_7.flip();
689 }
690
691 /// Increments the Byte by one.
692 ///
693 /// This method is used to increment the Byte by one. This means that the
694 /// value of the byte will increase by 1.
695 ///
696 /// This is a wrapping operator with no overflow. What this translates into
697 /// in practice is that if you increment the byte when its value is 255,
698 /// instead of getting an overflow error or the operation having no
699 /// effect, the value wraps around to 0.
700 ///
701 ///
702 /// # Examples
703 ///
704 ///
705 /// ## Simple Increment
706 /// ```
707 /// use brainfoamkit_lib::Byte;
708 ///
709 /// let mut byte = Byte::default(); // Byte: 0b00000000; Dec: 0; Hex: 0x00; Oct: 0o0
710 ///
711 /// byte.increment();
712 ///
713 /// assert_eq!(u8::from(&byte), 0b00000001); // Dec: 1; Hex: 0x01; Oct: 0o1
714 /// assert_eq!(byte.to_string(), "0x01");
715 ///
716 /// byte.increment();
717 ///
718 /// assert_eq!(u8::from(&byte), 0b00000010); // Dec: 2; Hex: 0x02; Oct: 0o2
719 /// assert_eq!(byte.to_string(), "0x02");
720 /// ```
721 ///
722 /// ## Increment at Boundary
723 ///
724 /// ```
725 /// use brainfoamkit_lib::Byte;
726 ///
727 /// let mut byte = Byte::from(255); // Byte: 0b11111111; Dec: 255; Hex: 0xFF; Oct: 0o377
728 ///
729 /// byte.increment();
730 ///
731 /// assert_eq!(u8::from(&byte), 0b00000000); // Byte: 0b00000000; Dec: 0; Hex: 0x00; Oct: 0o0
732 /// assert_eq!(byte.to_string(), "0x00");
733 /// ```
734 ///
735 /// # Side Effects
736 ///
737 /// This method will increment the Byte by one.
738 ///
739 /// # See Also
740 ///
741 /// * [`decrement()`](#method.decrement): Decrement the Byte by one.
742 /// * [`flip()`](#method.flip): Flip all of the Bit values in the Byte.
743 /// * [Integer Overflow](https://en.wikipedia.org/wiki/Integer_overflow): An
744 /// overview of the mathematics behind integer overflow
745 pub fn increment(&mut self) {
746 let mut carry = true;
747 let mut changes = Vec::new();
748
749 for (i, bit) in self.iter().enumerate() {
750 if !carry {
751 break;
752 }
753 if bit == Bit::One {
754 changes.push((i, false)); // Store the index and the new value
755 } else {
756 changes.push((i, true)); // Store the index and the new value
757 carry = false;
758 }
759 }
760
761 for (i, value) in changes {
762 if value {
763 self.set_bit(i);
764 } else {
765 self.unset_bit(i);
766 }
767 }
768 }
769
770 /// Decrements the Byte by one.
771 ///
772 /// This method is used to decrement the Byte by one. This means that the
773 /// value of the byte will decrease by 1.
774 ///
775 /// This is a wrapping operator with no overflow. What this translates into
776 /// in practice is that if you decrement the byte when its value is 0,
777 /// instead of getting an overflow error or the operation having no
778 /// effect, the value wraps around to 255.
779 ///
780 /// # Examples
781 ///
782 /// ## Simple Decrement
783 ///
784 /// ```
785 /// use brainfoamkit_lib::Byte;
786 ///
787 /// let mut byte = Byte::from(0b00000010); // Byte: 0b00000010; Dec: 2; Hex: 0x02; Oct: 0o2
788 ///
789 /// byte.decrement();
790 ///
791 /// assert_eq!(u8::from(&byte), 0b00000001); // Dec: 1; Hex: 0x01; Oct: 0o1
792 /// assert_eq!(byte.to_string(), "0x01");
793 ///
794 /// byte.decrement();
795 ///
796 /// assert_eq!(u8::from(&byte), 0b00000000); // Dec: 0; Hex: 0x00; Oct: 0o0
797 ///
798 /// assert_eq!(byte.to_string(), "0x00");
799 /// ```
800 ///
801 /// ## Decrement at Boundary
802 ///
803 /// ```
804 /// use brainfoamkit_lib::Byte;
805 ///
806 /// let mut byte = Byte::from(0); // Byte: 0b00000000; Dec: 0; Hex: 0x00; Oct: 0o0
807 ///
808 /// byte.decrement();
809 ///
810 /// assert_eq!(u8::from(&byte), 0b11111111); // Byte: 0b11111111; Dec: 255; Hex: 0xFF; Oct: 0o377
811 /// assert_eq!(byte.to_string(), "0xFF");
812 /// ```
813 ///
814 /// # Side Effects
815 ///
816 /// This method will decrement the Byte by one.
817 ///
818 /// # See Also
819 ///
820 /// * [`increment()`](#method.increment): Increment the Byte by one.
821 /// * [`flip()`](#method.flip): Flip all of the Bit values in the Byte.
822 pub fn decrement(&mut self) {
823 let mut borrow = true;
824 let mut changes = Vec::new();
825
826 for (i, bit) in self.iter().enumerate() {
827 if !borrow {
828 break;
829 }
830 if bit == Bit::Zero {
831 changes.push((i, true)); // Store the index and the new value
832 } else {
833 changes.push((i, false)); // Store the index and the new value
834 borrow = false;
835 }
836 }
837
838 for (i, value) in changes {
839 if value {
840 self.set_bit(i);
841 } else {
842 self.unset_bit(i);
843 }
844 }
845 }
846
847 /// Create an iterator over the Byte.
848 /// This allows the use of the `for` loop on the `Byte`.
849 ///
850 /// # Examples
851 ///
852 /// ```
853 /// use brainfoamkit_lib::Byte;
854 ///
855 /// let byte = Byte::from(0b11001010); // Dec: 10; Hex: 0xA; Oct: 0o12
856 ///
857 /// for bit in byte.iter() {
858 /// println!("{}", bit);
859 /// }
860 /// ```
861 #[must_use]
862 pub const fn iter(&self) -> IterableByte {
863 IterableByte::new(self)
864 }
865}
866
867impl Display for Byte {
868 /// Converts the Byte to a String.
869 ///
870 /// # Examples
871 ///
872 /// ```
873 /// use brainfoamkit_lib::Byte;
874 ///
875 /// let byte = Byte::from(0xAA); // Byte: 0b10101010; Dec: 170; Hex: 0xAA; Oct: 0o252
876 /// assert_eq!(byte.to_string(), "0xAA");
877 /// ```
878 ///
879 /// # Returns
880 ///
881 /// A String containing the value of the Byte.
882 ///
883 /// # See Also
884 ///
885 /// * [`to_u8()`](#method.to_u8)
886 /// * [`from_u8()`](#method.from_u8)
887 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
888 let number = u8::from(self);
889 write!(f, "{number:#04X}")
890 }
891}
892
893impl Default for Byte {
894 /// Creates a new Byte with all bits set to zero.
895 ///
896 /// # Examples
897 ///
898 /// ```
899 /// use brainfoamkit_lib::Byte;
900 ///
901 /// let byte = Byte::default(); // Byte: 0b00000000; Dec: 0; Hex: 0x00; Oct: 0o0
902 /// assert_eq!(u8::from(&byte), 0b00000000); // Dec: 0; Hex: 0x00; Oct: 0o0
903 /// assert_eq!(byte.to_string(), "0x00");
904 /// ```
905 ///
906 /// # Returns
907 ///
908 /// A Byte with all bits set to zero.
909 ///
910 /// # See Also
911 ///
912 /// * [`new()`](#method.new): Create a new Byte from individual Bit values.
913 /// * [`from_nybbles()`](#method.from_nybbles): Create a new Byte from two
914 /// Nybbles.
915 /// * [`from_u8()`](#method.from_u8): Create a new Byte from a u8.
916 /// * [`to_u8()`](#method.to_u8): Convert the Byte to a u8.
917 fn default() -> Self {
918 Self::new(
919 Bit::zero(), // Most significant bit
920 Bit::zero(),
921 Bit::zero(),
922 Bit::zero(),
923 Bit::zero(),
924 Bit::zero(),
925 Bit::zero(),
926 Bit::zero(), // Least significant bit
927 )
928 }
929}
930
931impl From<u8> for Byte {
932 /// Creates a new Byte from a u8.
933 ///
934 /// This method creates a new Byte from a u8.
935 ///
936 /// # Arguments
937 ///
938 /// * `n` - The u8 to create the Byte from.
939 ///
940 /// # Examples
941 ///
942 /// ```
943 /// use brainfoamkit_lib::Byte;
944 ///
945 /// let byte = Byte::from(0xAA); // Byte: 0b10101010; Dec: 170; Hex: 0xAA; Oct: 0o252
946 /// assert_eq!(u8::from(&byte), 0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
947 /// assert_eq!(byte.to_string(), "0xAA");
948 /// ```
949 ///
950 /// # Returns
951 ///
952 /// A Byte containing the value of the u8.
953 ///
954 /// # See Also
955 ///
956 /// * [`to_u8()`](#method.to_u8): Convert the Byte to a u8.
957 /// * [`from_nybbles()`](#method.from_nybbles): Create a new Byte from two
958 /// Nybbles.
959 /// * [`new()`](#method.new): Create a new Byte from individual Bit values.
960 fn from(n: u8) -> Self {
961 let mut byte = Self::default();
962
963 for i in 0..8 {
964 if n & (1 << i) != 0 {
965 byte.set_bit(i);
966 }
967 }
968
969 byte
970 }
971}
972
973impl From<&Byte> for u8 {
974 /// Converts the Byte to an 8-bit unsigned integer (u8).
975 ///
976 /// This method returns the value of the Byte as an 8-bit unsigned integer
977 /// (u8).
978 ///
979 /// # Examples
980 ///
981 /// ```
982 /// use brainfoamkit_lib::{
983 /// Bit,
984 /// Byte,
985 /// };
986 ///
987 /// let byte = Byte::new(
988 /// Bit::one(),
989 /// Bit::zero(),
990 /// Bit::one(),
991 /// Bit::zero(),
992 /// Bit::one(),
993 /// Bit::zero(),
994 /// Bit::one(),
995 /// Bit::zero(),
996 /// );
997 /// assert_eq!(u8::from(&byte), 0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
998 /// ```
999 ///
1000 /// # Returns
1001 ///
1002 /// An 8-bit unsigned integer (u8) containing the value of the Byte.
1003 ///
1004 /// # See Also
1005 ///
1006 /// * [`to_string()`](#method.to_string): Convert the Byte to a String.
1007 /// * [`from_u8()`](#method.from_u8): Create a new Byte from a u8.
1008 fn from(byte: &Byte) -> Self {
1009 let mut n = 0;
1010
1011 for i in 0..8 {
1012 if byte.get_bit(i) == Bit::One {
1013 n |= 1 << i;
1014 }
1015 }
1016
1017 n
1018 }
1019}
1020
1021impl Not for Byte {
1022 // The return type is Byte because the Not operation is in-place.
1023 type Output = Self;
1024
1025 /// Performs the Not operation on the Byte.
1026 ///
1027 /// This method is used to perform the Not operation on the Byte.
1028 /// This also allows the use of the `!` operator on the Byte.
1029 ///
1030 /// # Examples
1031 ///
1032 /// ```
1033 /// use brainfoamkit_lib::Byte;
1034 ///
1035 /// let mut byte = Byte::from(0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
1036 ///
1037 /// assert_eq!(u8::from(&byte), 0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
1038 /// assert_eq!(byte.to_string(), "0xAA");
1039 ///
1040 /// byte = !byte;
1041 ///
1042 /// assert_eq!(u8::from(&byte), 0b01010101); // Dec: 85; Hex: 0x55; Oct: 0o125
1043 /// assert_eq!(byte.to_string(), "0x55");
1044 /// ```
1045 ///
1046 /// # Returns
1047 ///
1048 /// This method inverts the Bit values in the Byte.
1049 ///
1050 /// # See Also
1051 ///
1052 /// * [`flip_bit()`](#method.flip_bit): Flip the Bit value at the specified
1053 /// index.
1054 /// * [`flip()`](#method.flip): Flip all of the Bit values in the Byte.
1055 fn not(self) -> Self::Output {
1056 let mut byte = self;
1057 byte.flip();
1058 byte
1059 }
1060}
1061
1062impl BitAnd for Byte {
1063 // The return type is Byte because the BitAnd operation is symmetric.
1064 type Output = Self;
1065
1066 /// Performs the Bitwise And operation on the Byte.
1067 ///
1068 /// This method is used to perform the Bitwise And operation on the Byte.
1069 /// This also allows the use of the `&` operator on the Byte.
1070 ///
1071 /// # Arguments
1072 ///
1073 /// * `rhs` - The right hand side of the `BitAnd` operation.
1074 ///
1075 /// # Examples
1076 ///
1077 /// ```
1078 /// use brainfoamkit_lib::Byte;
1079 ///
1080 /// let mut byte = Byte::from(0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
1081 ///
1082 /// assert_eq!(u8::from(&byte), 0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
1083 /// assert_eq!(byte.to_string(), "0xAA");
1084 ///
1085 /// byte = byte & Byte::from(0b01010101); // Dec: 85; Hex: 0x55; Oct: 0o125
1086 ///
1087 /// assert_eq!(u8::from(&byte), 0b00000000); // Dec: 0; Hex: 0x00; Oct: 0o0
1088 /// assert_eq!(byte.to_string(), "0x00");
1089 /// ```
1090 ///
1091 /// # Returns
1092 ///
1093 /// A Byte containing the result of a bitwise AND operation on the two
1094 /// Bytes.
1095 ///
1096 /// # See Also
1097 ///
1098 /// * [`bitor()`](#method.bitor): Perform a Bitwise Or operation on the
1099 /// Byte.
1100 /// * [`bitxor()`](#method.bitxor): Perform a Bitwise Xor operation on the
1101 /// Byte.
1102 /// * [`bitand_assign()`](#method.bitand_assign): Perform a Bitwise And
1103 /// Assignment operation on the Byte.
1104 /// * [`bitor_assign()`](#method.bitor_assign): Perform a Bitwise Or
1105 /// Assignment operation on the Byte.
1106 /// * [`bitxor_assign()`](#method.bitxor_assign): Perform a Bitwise Xor
1107 /// Assignment operation on the Byte.
1108 fn bitand(self, rhs: Self) -> Self::Output {
1109 let mut byte = self;
1110 byte.bit_0 &= rhs.bit_0;
1111 byte.bit_1 &= rhs.bit_1;
1112 byte.bit_2 &= rhs.bit_2;
1113 byte.bit_3 &= rhs.bit_3;
1114 byte.bit_4 &= rhs.bit_4;
1115 byte.bit_5 &= rhs.bit_5;
1116 byte.bit_6 &= rhs.bit_6;
1117 byte.bit_7 &= rhs.bit_7;
1118 byte
1119 }
1120}
1121
1122impl BitAndAssign for Byte {
1123 /// Performs the Bitwise And Assignment operation on the Byte.
1124 ///
1125 /// This method is used to perform the Bitwise And Assignment operation on
1126 /// the Byte. This also allows the use of the `&=` operator on the Byte.
1127 ///
1128 /// # Arguments
1129 ///
1130 /// * `rhs` - The right hand side of the `BitAnd` Assignment operation.
1131 ///
1132 /// # Examples
1133 ///
1134 /// ```
1135 /// use brainfoamkit_lib::Byte;
1136 ///
1137 /// let mut byte = Byte::from(0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
1138 ///
1139 /// assert_eq!(u8::from(&byte), 0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
1140 ///
1141 /// byte &= Byte::from(0b01010101); // Dec: 85; Hex: 0x55; Oct: 0o125
1142 ///
1143 /// assert_eq!(u8::from(&byte), 0); // Dec: 0; Hex: 0x00; Oct: 0o0
1144 /// ```
1145 ///
1146 /// # Side Effects
1147 ///
1148 /// This method performs a bitwise AND operation on the two Bytes, storing
1149 /// the result in the first Byte.
1150 ///
1151 /// # See Also
1152 ///
1153 /// * [`bitand()`](#method.bitand): Perform a Bitwise And operation on the
1154 /// Byte.
1155 /// * [`bitor()`](#method.bitor): Perform a Bitwise Or operation on the
1156 /// Byte.
1157 /// * [`bitxor()`](#method.bitxor): Perform a Bitwise Xor operation on the
1158 /// Byte.
1159 /// * [`bitor_assign()`](#method.bitor_assign): Perform a Bitwise Or
1160 /// Assignment operation on the Byte.
1161 /// * [`bitxor_assign()`](#method.bitxor_assign): Perform a Bitwise Xor
1162 /// Assignment operation on the Byte.
1163 fn bitand_assign(&mut self, rhs: Self) {
1164 self.bit_0 &= rhs.bit_0;
1165 self.bit_1 &= rhs.bit_1;
1166 self.bit_2 &= rhs.bit_2;
1167 self.bit_3 &= rhs.bit_3;
1168 self.bit_4 &= rhs.bit_4;
1169 self.bit_5 &= rhs.bit_5;
1170 self.bit_6 &= rhs.bit_6;
1171 self.bit_7 &= rhs.bit_7;
1172 }
1173}
1174
1175impl BitOr for Byte {
1176 // The return type is Byte because the BitOr operation is symmetric.
1177 type Output = Self;
1178
1179 /// Performs the Bitwise Or operation on the Byte.
1180 ///
1181 /// This method is used to perform the Bitwise Or operation on the Byte.
1182 /// This also allows the use of the `|` operator on the Byte.
1183 ///
1184 /// # Arguments
1185 ///
1186 /// * `rhs` - The right hand side of the Bitwise Or operation.
1187 ///
1188 /// # Examples
1189 ///
1190 /// ```
1191 /// use brainfoamkit_lib::Byte;
1192 ///
1193 /// let mut byte = Byte::from(0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
1194 ///
1195 /// assert_eq!(u8::from(&byte), 0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
1196 /// assert_eq!(byte.to_string(), "0xAA");
1197 ///
1198 /// byte = byte | Byte::from(0b01010101); // Dec: 85; Hex: 0x55; Oct: 0o125
1199 ///
1200 /// assert_eq!(u8::from(&byte), 0b11111111); // Dec: 255; Hex: 0xFF; Oct: 0o377
1201 /// assert_eq!(byte.to_string(), "0xFF");
1202 /// ```
1203 ///
1204 /// # Returns
1205 ///
1206 /// A Byte containing the result of a Bitwise Or operation on the two Bytes.
1207 ///
1208 /// # See Also
1209 ///
1210 /// * [`bitand()`](#method.bitand): Perform a Bitwise And operation on the
1211 /// Byte.
1212 /// * [`bitxor()`](#method.bitxor): Perform a Bitwise Xor operation on the
1213 /// Byte.
1214 /// * [`bitand_assign()`](#method.bitand_assign): Perform a Bitwise And
1215 /// Assignment operation on the Byte.
1216 /// * [`bitor_assign()`](#method.bitor_assign): Perform a Bitwise Or
1217 /// Assignment operation on the Byte.
1218 /// * [`bitxor_assign()`](#method.bitxor_assign): Perform a Bitwise Xor
1219 /// Assignment operation on the Byte.
1220 fn bitor(self, rhs: Self) -> Self::Output {
1221 let mut byte = self;
1222 byte.bit_0 |= rhs.bit_0;
1223 byte.bit_1 |= rhs.bit_1;
1224 byte.bit_2 |= rhs.bit_2;
1225 byte.bit_3 |= rhs.bit_3;
1226 byte.bit_4 |= rhs.bit_4;
1227 byte.bit_5 |= rhs.bit_5;
1228 byte.bit_6 |= rhs.bit_6;
1229 byte.bit_7 |= rhs.bit_7;
1230 byte
1231 }
1232}
1233
1234impl BitOrAssign for Byte {
1235 /// Performs the Bitwise Or Assignment operation on the Byte.
1236 ///
1237 /// This method is used to perform the Bitwise Or Assignment operation on
1238 /// the Byte. This also allows the use of the `|=` operator on the Byte.
1239 ///
1240 /// # Arguments
1241 ///
1242 /// * `rhs` - The right hand side of the Bitwise Or Assignment operation.
1243 ///
1244 /// # Examples
1245 ///
1246 /// ```
1247 /// use brainfoamkit_lib::Byte;
1248 ///
1249 /// let mut byte = Byte::from(0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
1250 ///
1251 /// assert_eq!(u8::from(&byte), 0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
1252 ///
1253 /// byte |= Byte::from(0b01010101); // Dec: 85; Hex: 0x55; Oct: 0o125
1254 ///
1255 /// assert_eq!(u8::from(&byte), 0b11111111); // Dec: 255; Hex: 0xFF; Oct: 0o377
1256 /// ```
1257 ///
1258 /// # Side Effects
1259 ///
1260 /// This method performs a Bitwise Or operation on the two Bytes, storing
1261 /// the result in the first Byte.
1262 ///
1263 /// # See Also
1264 ///
1265 /// * [`bitand()`](#method.bitand): Perform a Bitwise And operation on the
1266 /// Byte.
1267 /// * [`bitor()`](#method.bitor): Perform a Bitwise Or operation on the
1268 /// Byte.
1269 /// * [`bitxor()`](#method.bitxor): Perform a Bitwise Xor operation on the
1270 /// Byte.
1271 /// * [`bitand_assign()`](#method.bitand_assign): Perform a Bitwise And
1272 /// Assignment operation on the Byte.
1273 /// * [`bitxor_assign()`](#method.bitxor_assign): Perform a Bitwise Xor
1274 /// Assignment operation on the Byte.
1275 fn bitor_assign(&mut self, rhs: Self) {
1276 self.bit_0 |= rhs.bit_0;
1277 self.bit_1 |= rhs.bit_1;
1278 self.bit_2 |= rhs.bit_2;
1279 self.bit_3 |= rhs.bit_3;
1280 self.bit_4 |= rhs.bit_4;
1281 self.bit_5 |= rhs.bit_5;
1282 self.bit_6 |= rhs.bit_6;
1283 self.bit_7 |= rhs.bit_7;
1284 }
1285}
1286
1287impl BitXor for Byte {
1288 // The return type is Byte because the BitXor operation is symmetric.
1289 type Output = Self;
1290
1291 /// Performs the Bitwise Xor operation on the Byte.
1292 ///
1293 /// This method is used to perform the Bitwise Xor operation on the Byte.
1294 /// This also allows the use of the `^` operator on the Byte.
1295 ///
1296 /// # Arguments
1297 ///
1298 /// * `rhs` - The right hand side of the Bitwise Xor operation.
1299 ///
1300 /// # Examples
1301 ///
1302 /// ```
1303 /// use brainfoamkit_lib::Byte;
1304 ///
1305 /// let mut byte = Byte::from(0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
1306 ///
1307 /// assert_eq!(u8::from(&byte), 0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
1308 ///
1309 /// byte = byte ^ Byte::from(0b01010101); // Dec: 85; Hex: 0x55; Oct: 0o125
1310 ///
1311 /// assert_eq!(u8::from(&byte), 0b11111111); // Dec: 255; Hex: 0xFF; Oct: 0o377
1312 /// ```
1313 ///
1314 /// # Returns
1315 ///
1316 /// A Byte containing the result of a Bitwise Xor operation on the two
1317 /// Bytes.
1318 ///
1319 /// # See Also
1320 ///
1321 /// * [`bitand()`](#method.bitand): Perform a Bitwise And operation on the
1322 /// Byte.
1323 /// * [`bitor()`](#method.bitor): Perform a Bitwise Or operation on the
1324 /// Byte.
1325 /// * [`bitand_assign()`](#method.bitand_assign): Perform a Bitwise And
1326 /// Assignment operation on the Byte.
1327 /// * [`bitor_assign()`](#method.bitor_assign): Perform a Bitwise Or
1328 /// Assignment operation on the Byte.
1329 /// * [`bitxor_assign()`](#method.bitxor_assign): Perform a Bitwise Xor
1330 /// Assignment operation on the Byte.
1331 fn bitxor(self, rhs: Self) -> Self::Output {
1332 let mut byte = self;
1333 byte.bit_0 ^= rhs.bit_0;
1334 byte.bit_1 ^= rhs.bit_1;
1335 byte.bit_2 ^= rhs.bit_2;
1336 byte.bit_3 ^= rhs.bit_3;
1337 byte.bit_4 ^= rhs.bit_4;
1338 byte.bit_5 ^= rhs.bit_5;
1339 byte.bit_6 ^= rhs.bit_6;
1340 byte.bit_7 ^= rhs.bit_7;
1341 byte
1342 }
1343}
1344
1345impl BitXorAssign for Byte {
1346 /// Performs the Bitwise Xor Assignment operation on the Byte.
1347 ///
1348 /// This method is used to perform the Bitwise Xor Assignment operation on
1349 /// the Byte. This also allows the use of the `^=` operator on the Byte.
1350 ///
1351 /// # Arguments
1352 ///
1353 /// * `rhs` - The right hand side of the Bitwise Xor Assignment operation.
1354 ///
1355 /// # Examples
1356 ///
1357 /// ```
1358 /// use brainfoamkit_lib::Byte;
1359 ///
1360 /// let mut byte = Byte::from(0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
1361 ///
1362 /// assert_eq!(u8::from(&byte), 0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
1363 ///
1364 /// byte ^= Byte::from(0b01010101);
1365 ///
1366 /// assert_eq!(u8::from(&byte), 0b11111111); // Dec: 255; Hex: 0xFF; Oct: 0o377
1367 /// ```
1368 ///
1369 /// # Side Effects
1370 ///
1371 /// This method performs a Bitwise Xor operation on the two Bytes, storing
1372 /// the result in the first Byte.
1373 ///
1374 /// # See Also
1375 ///
1376 /// * [`bitand()`](#method.bitand): Perform a Bitwise And operation on the
1377 /// Byte.
1378 /// * [`bitor()`](#method.bitor): Perform a Bitwise Or operation on the
1379 /// Byte.
1380 /// * [`bitxor()`](#method.bitxor): Perform a Bitwise Xor operation on the
1381 /// Byte.
1382 /// * [`bitand_assign()`](#method.bitand_assign): Perform a Bitwise And
1383 /// Assignment operation on the Byte.
1384 /// * [`bitor_assign()`](#method.bitor_assign): Perform a Bitwise Or
1385 /// Assignment operation on the Byte.
1386 fn bitxor_assign(&mut self, rhs: Self) {
1387 self.bit_0 ^= rhs.bit_0;
1388 self.bit_1 ^= rhs.bit_1;
1389 self.bit_2 ^= rhs.bit_2;
1390 self.bit_3 ^= rhs.bit_3;
1391 self.bit_4 ^= rhs.bit_4;
1392 self.bit_5 ^= rhs.bit_5;
1393 self.bit_6 ^= rhs.bit_6;
1394 self.bit_7 ^= rhs.bit_7;
1395 }
1396}
1397
1398/// `IntoIterator` implementation for a reference to a `Byte`.
1399///
1400/// This implementation allows a `Byte` reference to be converted into an
1401/// iterator. The iterator will yield `Bit` items.
1402impl<'a> IntoIterator for &'a Byte {
1403 /// The type of the iterator that will be returned. It's an `IterableByte`
1404 /// with the same lifetime as the `Byte` reference.
1405 type IntoIter = IterableByte<'a>;
1406 /// The type of the items that will be returned when iterating over the
1407 /// `Byte` reference. In this case, it's a `Bit`.
1408 type Item = Bit;
1409
1410 /// Converts the `Byte` reference into an `IterableByte` iterator.
1411 ///
1412 /// # Returns
1413 ///
1414 /// An `IterableByte` iterator with the same lifetime as the `Byte`
1415 /// reference.
1416 fn into_iter(self) -> Self::IntoIter {
1417 self.iter()
1418 }
1419}
1420
1421#[cfg(test)]
1422mod tests {
1423 use super::*;
1424
1425 #[test]
1426 fn test_display() {
1427 let byte = Byte::from(0b10101010);
1428 assert_eq!(format!("{}", byte), "0xAA");
1429 }
1430
1431 #[test]
1432 fn test_default() {
1433 let byte = Byte::default();
1434 assert_eq!(u8::from(&byte), 0b00000000);
1435 }
1436
1437 #[test]
1438 fn test_from_u8_zero() {
1439 let byte = Byte::from(0);
1440 assert_eq!(byte, Byte::default());
1441 }
1442
1443 #[test]
1444 fn test_from_u8_one() {
1445 let byte = Byte::from(1);
1446 let mut expected = Byte::default();
1447 expected.set_bit(0);
1448 assert_eq!(byte, expected);
1449 }
1450
1451 #[test]
1452 fn test_from_u8_max() {
1453 let byte = Byte::from(u8::MAX);
1454 let mut expected = Byte::default();
1455 for i in 0..8 {
1456 expected.set_bit(i);
1457 }
1458 assert_eq!(byte, expected);
1459 }
1460
1461 #[test]
1462 fn test_from_nybbles_ones() {
1463 let high_nybble = Nybble::from(15);
1464 let low_nybble = Nybble::from(15);
1465 let byte = Byte::from_nybbles(high_nybble, low_nybble);
1466 assert_eq!(u8::from(&byte), 0b11111111);
1467 }
1468
1469 #[test]
1470 fn test_from_nybbles_zeros() {
1471 let high_nybble = Nybble::default();
1472 let low_nybble = Nybble::default();
1473 let byte = Byte::from_nybbles(high_nybble, low_nybble);
1474 assert_eq!(u8::from(&byte), 0);
1475 }
1476
1477 #[test]
1478 fn test_from_nybbles_alternating() {
1479 let high_nybble = Nybble::from(0b1010);
1480 let low_nybble = Nybble::from(0b0101);
1481 let byte = Byte::from_nybbles(high_nybble, low_nybble);
1482 assert_eq!(u8::from(&byte), 0b10100101);
1483
1484 let high_nybble = Nybble::from(0b0101);
1485 let low_nybble = Nybble::from(0b1010);
1486 let byte = Byte::from_nybbles(high_nybble, low_nybble);
1487 assert_eq!(u8::from(&byte), 0b01011010);
1488 }
1489
1490 #[test]
1491 fn test_to_u8_all_zeros() {
1492 let byte = Byte::default();
1493 assert_eq!(u8::from(&byte), 0b00000000);
1494 }
1495
1496 #[test]
1497 fn test_to_u8_all_ones() {
1498 let byte = Byte::from(0b11111111);
1499 assert_eq!(u8::from(&byte), 0b11111111);
1500 }
1501
1502 #[test]
1503 fn test_to_u8_alternating_raw() {
1504 let byte = Byte::new(
1505 Bit::one(),
1506 Bit::zero(),
1507 Bit::one(),
1508 Bit::zero(),
1509 Bit::one(),
1510 Bit::zero(),
1511 Bit::one(),
1512 Bit::zero(),
1513 );
1514 assert_eq!(u8::from(&byte), 0b10101010);
1515 }
1516
1517 #[test]
1518 fn test_to_u8_alternating() {
1519 let byte = Byte::from(0b10101010);
1520 assert_eq!(u8::from(&byte), 0b10101010);
1521 }
1522
1523 #[test]
1524 fn test_to_u8_random() {
1525 let byte = Byte::from(0b11001100);
1526 assert_eq!(u8::from(&byte), 0b11001100);
1527 }
1528
1529 #[test]
1530 fn test_get_high_nybble_all_zeros() {
1531 let byte = Byte::default();
1532 let nybble = byte.get_high_nybble();
1533 assert_eq!(u8::from(&nybble), 0b0000);
1534 }
1535
1536 #[test]
1537 fn test_get_high_nybble_all_ones() {
1538 let byte = Byte::from(0b11111111);
1539 let nybble = byte.get_high_nybble();
1540 assert_eq!(u8::from(&nybble), 15);
1541 }
1542
1543 #[test]
1544 fn test_get_high_nybble_alternating() {
1545 let byte = Byte::from(0b10101010);
1546 let nybble = byte.get_high_nybble();
1547 assert_eq!(u8::from(&nybble), 0b1010);
1548 }
1549
1550 #[test]
1551 fn test_get_high_nybble_random() {
1552 let byte = Byte::from(0b11001100);
1553 let nybble = byte.get_high_nybble();
1554 assert_eq!(u8::from(&nybble), 0b1100);
1555 }
1556
1557 #[test]
1558 fn test_get_low_nybble_all_zeros() {
1559 let byte = Byte::default();
1560 let nybble = byte.get_low_nybble();
1561 assert_eq!(u8::from(&nybble), 0b0000);
1562 }
1563
1564 #[test]
1565 fn test_get_low_nybble_all_ones() {
1566 let byte = Byte::from(0b11111111);
1567 let nybble = byte.get_low_nybble();
1568 assert_eq!(u8::from(&nybble), 15);
1569 }
1570
1571 #[test]
1572 fn test_get_low_nybble_alternating() {
1573 let byte = Byte::from(0b10101010);
1574 let nybble = byte.get_low_nybble();
1575 assert_eq!(u8::from(&nybble), 0b1010);
1576 }
1577
1578 #[test]
1579 fn test_get_low_nybble_random() {
1580 let byte = Byte::from(0b11001100);
1581 let nybble = byte.get_low_nybble();
1582 assert_eq!(u8::from(&nybble), 0b1100);
1583 }
1584
1585 #[test]
1586 fn test_set_bit_valid() {
1587 let mut byte = Byte::default();
1588 byte.set_bit(0);
1589 assert_eq!(u8::from(&byte), 1);
1590
1591 let mut byte = Byte::default();
1592 byte.set_bit(1);
1593 assert_eq!(u8::from(&byte), 2);
1594
1595 let mut byte = Byte::default();
1596 byte.set_bit(2);
1597 assert_eq!(u8::from(&byte), 4);
1598
1599 let mut byte = Byte::default();
1600 byte.set_bit(3);
1601 assert_eq!(u8::from(&byte), 0b00001000);
1602
1603 let mut byte = Byte::default();
1604 byte.set_bit(4);
1605 assert_eq!(u8::from(&byte), 0b00010000);
1606
1607 let mut byte = Byte::default();
1608 byte.set_bit(5);
1609 assert_eq!(u8::from(&byte), 0b00100000);
1610
1611 let mut byte = Byte::default();
1612 byte.set_bit(6);
1613 assert_eq!(u8::from(&byte), 0b01000000);
1614
1615 let mut byte = Byte::default();
1616 byte.set_bit(7);
1617 assert_eq!(u8::from(&byte), 0b10000000);
1618 }
1619
1620 #[test]
1621 #[should_panic(expected = "Index out of bounds")]
1622 fn test_set_bit_out_of_bounds() {
1623 let mut byte = Byte::default();
1624 byte.set_bit(8);
1625 }
1626
1627 #[test]
1628 fn test_unset_bit_valid() {
1629 let mut byte = Byte::from(0b11111111);
1630 byte.unset_bit(0);
1631 assert_eq!(u8::from(&byte), 0b11111110);
1632
1633 let mut byte = Byte::from(0b11111111);
1634 byte.unset_bit(1);
1635 assert_eq!(u8::from(&byte), 0b11111101);
1636
1637 let mut byte = Byte::from(0b11111111);
1638 byte.unset_bit(2);
1639 assert_eq!(u8::from(&byte), 0b11111011);
1640
1641 let mut byte = Byte::from(0b11111111);
1642 byte.unset_bit(3);
1643 assert_eq!(u8::from(&byte), 0b11110111);
1644
1645 let mut byte = Byte::from(0b11111111);
1646 byte.unset_bit(4);
1647 assert_eq!(u8::from(&byte), 0b11101111);
1648
1649 let mut byte = Byte::from(0b11111111);
1650 byte.unset_bit(5);
1651 assert_eq!(u8::from(&byte), 0b11011111);
1652
1653 let mut byte = Byte::from(0b11111111);
1654 byte.unset_bit(6);
1655 assert_eq!(u8::from(&byte), 0b10111111);
1656
1657 let mut byte = Byte::from(0b11111111);
1658 byte.unset_bit(7);
1659 assert_eq!(u8::from(&byte), 0b01111111);
1660 }
1661
1662 #[test]
1663 #[should_panic(expected = "Index out of bounds")]
1664 fn test_unset_bit_out_of_bounds() {
1665 let mut byte = Byte::from(0b00000000);
1666 byte.unset_bit(8);
1667 }
1668
1669 #[test]
1670 fn test_get_bit_valid() {
1671 let byte = Byte::from(0b01010101);
1672 assert_eq!(byte.get_bit(0), Bit::One);
1673 assert_eq!(byte.get_bit(1), Bit::Zero);
1674 assert_eq!(byte.get_bit(2), Bit::One);
1675 assert_eq!(byte.get_bit(3), Bit::Zero);
1676 assert_eq!(byte.get_bit(4), Bit::One);
1677 assert_eq!(byte.get_bit(5), Bit::Zero);
1678 assert_eq!(byte.get_bit(6), Bit::One);
1679 assert_eq!(byte.get_bit(7), Bit::Zero);
1680 }
1681
1682 #[test]
1683 #[should_panic(expected = "Index out of bounds")]
1684 fn test_get_bit_out_of_bounds() {
1685 let byte = Byte::from(0b00000000);
1686 let _ = byte.get_bit(8);
1687 }
1688
1689 #[test]
1690 fn test_flip_bit_valid() {
1691 let mut byte = Byte::from(0b01010101);
1692 byte.flip_bit(0);
1693 assert_eq!(u8::from(&byte), 0b01010100);
1694 byte.flip_bit(1);
1695 assert_eq!(u8::from(&byte), 0b01010110);
1696 byte.flip_bit(2);
1697 assert_eq!(u8::from(&byte), 0b01010010);
1698 byte.flip_bit(3);
1699 assert_eq!(u8::from(&byte), 0b01011010);
1700 byte.flip_bit(4);
1701 assert_eq!(u8::from(&byte), 0b01001010);
1702 byte.flip_bit(5);
1703 assert_eq!(u8::from(&byte), 0b01101010);
1704 byte.flip_bit(6);
1705 assert_eq!(u8::from(&byte), 0b00101010);
1706 byte.flip_bit(7);
1707 assert_eq!(u8::from(&byte), 0b10101010);
1708 }
1709
1710 #[test]
1711 #[should_panic(expected = "Index out of bounds")]
1712 fn test_flip_bit_out_of_bounds() {
1713 let mut byte = Byte::from(0b00000000);
1714 byte.flip_bit(8);
1715 }
1716
1717 #[test]
1718 fn test_flip_all_bits() {
1719 let mut byte = Byte::from(0b11111111);
1720 byte.flip();
1721 assert_eq!(u8::from(&byte), 0b00000000);
1722 }
1723
1724 #[test]
1725 fn test_flip_no_bits() {
1726 let mut byte = Byte::from(0b00000000);
1727 byte.flip();
1728 assert_eq!(u8::from(&byte), 0b11111111);
1729 }
1730
1731 #[test]
1732 fn test_flip_odd_bits() {
1733 let mut byte = Byte::from(0b01010101);
1734 byte.flip();
1735 assert_eq!(u8::from(&byte), 0b10101010);
1736 }
1737
1738 #[test]
1739 fn test_flip_even_bits() {
1740 let mut byte = Byte::from(0b10101010);
1741 byte.flip();
1742 assert_eq!(u8::from(&byte), 0b01010101);
1743 }
1744
1745 #[test]
1746 fn test_flip_alternating_bits() {
1747 let mut byte = Byte::from(0b01010101);
1748 byte.flip();
1749 assert_eq!(u8::from(&byte), 0b10101010);
1750 byte.flip();
1751 assert_eq!(u8::from(&byte), 0b01010101);
1752 }
1753
1754 #[test]
1755 fn test_bitnot() {
1756 let byte = Byte::from(0b10101010);
1757 let result = !byte;
1758 assert_eq!(u8::from(&result), 0b01010101);
1759
1760 let byte = Byte::from(0b11110000);
1761 let result = !byte;
1762 assert_eq!(u8::from(&result), 0b00001111);
1763
1764 let byte = Byte::from(0b00000000);
1765 let result = !byte;
1766 assert_eq!(u8::from(&result), 0b11111111);
1767 }
1768
1769 #[test]
1770 fn test_bitand() {
1771 let byte1 = Byte::from(0b10101010);
1772 let byte2 = Byte::from(0b11001100);
1773 let result = byte1 & byte2;
1774 assert_eq!(u8::from(&result), 0b10001000);
1775
1776 let byte1 = Byte::from(0b11110000);
1777 let byte2 = Byte::from(0b00001111);
1778 let result = byte1 & byte2;
1779 assert_eq!(u8::from(&result), 0b00000000);
1780
1781 let byte1 = Byte::from(0b11111111);
1782 let byte2 = Byte::from(0b11111111);
1783 let result = byte1 & byte2;
1784 assert_eq!(u8::from(&result), 0b11111111);
1785 }
1786
1787 #[test]
1788 fn test_bitand_assign() {
1789 let mut byte1 = Byte::from(0b10101010);
1790 let byte2 = Byte::from(0b11001100);
1791 byte1 &= byte2;
1792 assert_eq!(u8::from(&byte1), 0b10001000);
1793
1794 let mut byte1 = Byte::from(0b11110000);
1795 let byte2 = Byte::from(0b00001111);
1796 byte1 &= byte2;
1797 assert_eq!(u8::from(&byte1), 0b00000000);
1798
1799 let mut byte1 = Byte::from(0b11111111);
1800 let byte2 = Byte::from(0b11111111);
1801 byte1 &= byte2;
1802 assert_eq!(u8::from(&byte1), 0b11111111);
1803 }
1804
1805 #[test]
1806 fn test_bitor() {
1807 let byte1 = Byte::from(0b10101010);
1808 let byte2 = Byte::from(0b11001100);
1809 let result = byte1 | byte2;
1810 assert_eq!(u8::from(&result), 0b11101110);
1811
1812 let byte1 = Byte::from(0b11110000);
1813 let byte2 = Byte::from(0b00001111);
1814 let result = byte1 | byte2;
1815 assert_eq!(u8::from(&result), 0b11111111);
1816
1817 let byte1 = Byte::from(0b00000000);
1818 let byte2 = Byte::from(0b11111111);
1819 let result = byte1 | byte2;
1820 assert_eq!(u8::from(&result), 0b11111111);
1821 }
1822
1823 #[test]
1824 fn test_bitor_assign() {
1825 let mut byte1 = Byte::from(0b10101010);
1826 let byte2 = Byte::from(0b11001100);
1827 byte1 |= byte2;
1828 assert_eq!(u8::from(&byte1), 0b11101110);
1829
1830 let mut byte1 = Byte::from(0b11110000);
1831 let byte2 = Byte::from(0b00001111);
1832 byte1 |= byte2;
1833 assert_eq!(u8::from(&byte1), 0b11111111);
1834
1835 let mut byte1 = Byte::from(0b00000000);
1836 let byte2 = Byte::from(0b11111111);
1837 byte1 |= byte2;
1838 assert_eq!(u8::from(&byte1), 0b11111111);
1839 }
1840
1841 #[test]
1842 fn test_bitxor() {
1843 let byte1 = Byte::from(0b10101010);
1844 let byte2 = Byte::from(0b11001100);
1845 let result = byte1 ^ byte2;
1846 assert_eq!(u8::from(&result), 0b01100110);
1847
1848 let byte1 = Byte::from(0b11110000);
1849 let byte2 = Byte::from(0b00001111);
1850 let result = byte1 ^ byte2;
1851 assert_eq!(u8::from(&result), 0b11111111);
1852
1853 let byte1 = Byte::from(0b00000000);
1854 let byte2 = Byte::from(0b11111111);
1855 let result = byte1 ^ byte2;
1856 assert_eq!(u8::from(&result), 0b11111111);
1857 }
1858
1859 #[test]
1860 fn test_bitxor_assign() {
1861 let mut byte1 = Byte::from(0b10101010);
1862 let byte2 = Byte::from(0b11001100);
1863 byte1 ^= byte2;
1864 assert_eq!(u8::from(&byte1), 0b01100110);
1865
1866 let mut byte1 = Byte::from(0b11110000);
1867 let byte2 = Byte::from(0b00001111);
1868 byte1 ^= byte2;
1869 assert_eq!(u8::from(&byte1), 0b11111111);
1870
1871 let mut byte1 = Byte::from(0b00000000);
1872 let byte2 = Byte::from(0b11111111);
1873 byte1 ^= byte2;
1874 assert_eq!(u8::from(&byte1), 0b11111111);
1875 }
1876
1877 #[test]
1878 fn test_increment() {
1879 let mut byte = Byte::default();
1880 byte.increment();
1881 assert_eq!(u8::from(&byte), 1);
1882
1883 let mut byte = Byte::from(0b11111111);
1884 byte.increment();
1885 assert_eq!(u8::from(&byte), 0);
1886
1887 let mut byte = Byte::from(0b00001111);
1888 byte.increment();
1889 assert_eq!(u8::from(&byte), 0b00010000);
1890
1891 let mut byte = Byte::from(0b11110000);
1892 byte.increment();
1893 assert_eq!(u8::from(&byte), 0b11110001);
1894 }
1895
1896 #[test]
1897 fn test_decrement() {
1898 let mut byte = Byte::default();
1899 byte.decrement();
1900 assert_eq!(u8::from(&byte), 0b11111111);
1901
1902 let mut byte = Byte::from(0b11111111);
1903 byte.decrement();
1904 assert_eq!(u8::from(&byte), 0b11111110);
1905
1906 let mut byte = Byte::from(0b00001111);
1907 byte.decrement();
1908 assert_eq!(u8::from(&byte), 0b00001110);
1909
1910 let mut byte = Byte::from(0b11110000);
1911 byte.decrement();
1912 assert_eq!(u8::from(&byte), 0b11101111);
1913 }
1914
1915 #[test]
1916 fn test_iter() {
1917 let byte = Byte::from(0b10101010);
1918 let mut iter = byte.iter();
1919 assert_eq!(iter.next(), Some(Bit::Zero)); // zeroth Bit
1920 assert_eq!(iter.next(), Some(Bit::One)); // first Bit
1921 assert_eq!(iter.next(), Some(Bit::Zero)); // second Bit
1922 assert_eq!(iter.next(), Some(Bit::One)); // third Bit
1923 assert_eq!(iter.next(), Some(Bit::Zero)); // fourth Bit
1924 assert_eq!(iter.next(), Some(Bit::One)); // fifth Bit
1925 assert_eq!(iter.next(), Some(Bit::Zero)); // sixth Bit
1926 assert_eq!(iter.next(), Some(Bit::One));
1927 assert_eq!(iter.next(), None);
1928 }
1929
1930 #[test]
1931 fn test_into_iter() {
1932 let byte = Byte::from(0b10101010); // Assuming Byte::from exists
1933 let mut iter = (&byte).into_iter();
1934
1935 // Assuming Bit is an enum with variants Zero and One
1936 assert_eq!(iter.next(), Some(Bit::Zero));
1937 assert_eq!(iter.next(), Some(Bit::One));
1938 assert_eq!(iter.next(), Some(Bit::Zero));
1939 assert_eq!(iter.next(), Some(Bit::One));
1940 assert_eq!(iter.next(), Some(Bit::Zero));
1941 assert_eq!(iter.next(), Some(Bit::One));
1942 assert_eq!(iter.next(), Some(Bit::Zero));
1943 assert_eq!(iter.next(), Some(Bit::One));
1944 assert_eq!(iter.next(), None); // Ensure the iterator is exhausted
1945 }
1946
1947 #[test]
1948 fn test_into_iter_empty_byte() {
1949 let byte = Byte::from(0b00000000); // Assuming Byte::from exists
1950 let mut iter = (&byte).into_iter();
1951
1952 // Assuming Bit is an enum with variants Zero and One
1953 assert_eq!(iter.next(), Some(Bit::Zero));
1954 assert_eq!(iter.next(), Some(Bit::Zero));
1955 assert_eq!(iter.next(), Some(Bit::Zero));
1956 assert_eq!(iter.next(), Some(Bit::Zero));
1957 assert_eq!(iter.next(), Some(Bit::Zero));
1958 assert_eq!(iter.next(), Some(Bit::Zero));
1959 assert_eq!(iter.next(), Some(Bit::Zero));
1960 assert_eq!(iter.next(), Some(Bit::Zero));
1961 assert_eq!(iter.next(), None); // Ensure the iterator is exhausted
1962 }
1963}