rustgym/leetcode/_693_binary_number_with_alternating_bits.rs
1struct Solution;
2
3impl Solution {
4 fn has_alternating_bits(n: i32) -> bool {
5 let x = (n >> 1) ^ n;
6 (x + 1) & x == 0
7 }
8}
9
10#[test]
11fn test() {
12 assert_eq!(Solution::has_alternating_bits(5), true);
13 assert_eq!(Solution::has_alternating_bits(7), false);
14 assert_eq!(Solution::has_alternating_bits(11), false);
15 assert_eq!(Solution::has_alternating_bits(1), true);
16}