-
-
Save zoeisnowooze/7af9de1aa8425ae565895545633bebeb to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//! 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