Skip to content

Instantly share code, notes, and snippets.

@shadeglare
Last active March 30, 2023 12:28
Show Gist options
  • Save shadeglare/2acff92da0e4c6f4b29c1d9ebbafa79c to your computer and use it in GitHub Desktop.
Save shadeglare/2acff92da0e4c6f4b29c1d9ebbafa79c to your computer and use it in GitHub Desktop.
pub trait BitSelect {
fn bitselect(self, right: Self, filter: Self) -> Self;
}
macro_rules! impl_bitselect {
($first:ty, $($rest:ty),+) => {
impl_bitselect!($first);
impl_bitselect!($($rest),+);
};
($ty:ty) => {
impl BitSelect for $ty {
#[inline(always)]
fn bitselect(self, right: Self, filter: Self) -> Self {
(self & !filter) ^ (right & filter)
}
}
};
}
impl_bitselect!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment