Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created October 11, 2021 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rust-play/50aee14a5a83f58ce82d0745f3d766a7 to your computer and use it in GitHub Desktop.
Save rust-play/50aee14a5a83f58ce82d0745f3d766a7 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
//! Checks if an integer is an odious number.
/// Determine if an integer is an odious number.
///
/// 14 is an odious number because it has an odd number of 1s in its binary
/// expansion 1110. Conversely, 5 isn't an odious number. It has an even number
/// of 1s in its binary expansion 101 and so is called an evil number.
fn is_odious(num: i64) -> bool {
let mut num = num;
let mut sum = false;
while num > 0 {
sum ^= num & 1 == 1;
num >>= 1;
}
sum
}
fn main() {
println!("14 -> {}", is_odious(14));
println!("5 -> {}", is_odious(5));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment