RIP-Comm / clementine

Gameboy Advance emulator.
MIT License
49 stars 13 forks source link

Add bit manipulation methods #39

Closed simo498 closed 2 years ago

simo498 commented 2 years ago

It adds some methods to check and set single bits. For now there aren't methods to handle multiple bits masks, and it's limited to u32 (due to the assence of a common Integer trait in rust).

Refers to #33

guerinoni commented 2 years ago

@simo498 here I appreciate more feedback also from @AlessandroGrassi99 and @gallottino But sure I prefer this kind of api not for a struct Bitwise but as implementation of u32 or such... In this way you can do my_number.toggle_bit(15) and this will toggle the bit 15. I have some doubt also if is better is_set/is_reset but let's discuss :D

simo498 commented 2 years ago

@simo498 here I appreciate more feedback also from @AlessandroGrassi99 and @gallottino But sure I prefer this kind of api not for a struct Bitwise but as implementation of u32 or such... In this way you can do my_number.toggle_bit(15) and this will toggle the bit 15. I have some doubt also if is better is_set/is_reset but let's discuss :D

Yeah it's an idea. Another is to create a wrapper like Bitwise(u32). Anyway let's hear the opinion of other people on which implementation we should uses. I also think something more direct like u32::get_bit or whatever would be more readable

simo498 commented 2 years ago

I implemented the methods to u32 directly, so they can be called by just adding use crate::bitwise::Bits, i also added rand for testing, but it can be useful in the future as well