Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// Utility impl on u8's for some kind of "bit pattern matching".
- ///
- /// Consider performance implications (If any) later.
- pub trait AsBoolSlice {
- fn is_set(self, mask: u8) -> bool;
- fn is_set_n(self, n: u8) -> bool;
- fn as_bools(self) -> [bool; 8];
- }
- impl AsBoolSlice for u8 {
- #[inline]
- fn is_set(self, mask: u8) -> bool {
- (self & mask) == mask
- }
- #[inline]
- fn is_set_n(self, bit_number: u8) -> bool {
- self.is_set(1u8 << bit_number)
- }
- #[inline]
- fn as_bools(self) -> [bool; 8] {
- [
- self.is_set_n(0),
- self.is_set_n(1),
- self.is_set_n(2),
- self.is_set_n(3),
- self.is_set_n(4),
- self.is_set_n(5),
- self.is_set_n(6),
- self.is_set_n(7),
- ]
- }
- }
- fn main() {
- const size: usize = 32; // bytes
- let mut native_pixels = [0u8; size*3];
- let chip8_pixels = [0b0010_1000u8; size/8];
- for (byte, bools) in chip8_pixels.iter().map(|byte| byte.as_bools()).enumerate() {
- for (bit, bool) in bools.iter().enumerate() {
- native_pixels[(byte*8 + bit) * 3] = if *bool {
- 0
- } else {
- 255
- };
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement