Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. /// Utility impl on u8's for some kind of "bit pattern matching".
  2. ///
  3. /// Consider performance implications (If any) later.
  4. pub trait AsBoolSlice {
  5. fn is_set(self, mask: u8) -> bool;
  6. fn is_set_n(self, n: u8) -> bool;
  7. fn as_bools(self) -> [bool; 8];
  8. }
  9.  
  10. impl AsBoolSlice for u8 {
  11. #[inline]
  12. fn is_set(self, mask: u8) -> bool {
  13. (self & mask) == mask
  14. }
  15.  
  16. #[inline]
  17. fn is_set_n(self, bit_number: u8) -> bool {
  18. self.is_set(1u8 << bit_number)
  19. }
  20.  
  21. #[inline]
  22. fn as_bools(self) -> [bool; 8] {
  23. [
  24. self.is_set_n(0),
  25. self.is_set_n(1),
  26. self.is_set_n(2),
  27. self.is_set_n(3),
  28. self.is_set_n(4),
  29. self.is_set_n(5),
  30. self.is_set_n(6),
  31. self.is_set_n(7),
  32. ]
  33. }
  34. }
  35. fn main() {
  36. const size: usize = 32; // bytes
  37. let mut native_pixels = [0u8; size*3];
  38. let chip8_pixels = [0b0010_1000u8; size/8];
  39. for (byte, bools) in chip8_pixels.iter().map(|byte| byte.as_bools()).enumerate() {
  40. for (bit, bool) in bools.iter().enumerate() {
  41. native_pixels[(byte*8 + bit) * 3] = if *bool {
  42. 0
  43. } else {
  44. 255
  45. };
  46. }
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement