|
| 1 | +use num_traits::CheckedAdd; |
| 2 | + |
| 3 | +pub fn binary_to_decimal(binary: &str) -> Option<u128> { |
| 4 | + if binary.len() > 128 { |
| 5 | + return None; |
| 6 | + } |
| 7 | + let mut num = 0; |
| 8 | + let mut idx_val = 1; |
| 9 | + for bit in binary.chars().rev() { |
| 10 | + match bit { |
| 11 | + '1' => { |
| 12 | + if let Some(sum) = num.checked_add(&idx_val) { |
| 13 | + num = sum; |
| 14 | + } else { |
| 15 | + return None; |
| 16 | + } |
| 17 | + } |
| 18 | + '0' => {} |
| 19 | + _ => return None, |
| 20 | + } |
| 21 | + idx_val <<= 1; |
| 22 | + } |
| 23 | + Some(num) |
| 24 | +} |
| 25 | + |
| 26 | +#[cfg(test)] |
| 27 | +mod tests { |
| 28 | + use super::binary_to_decimal; |
| 29 | + |
| 30 | + #[test] |
| 31 | + fn basic_binary_to_decimal() { |
| 32 | + assert_eq!(binary_to_decimal("0000000110"), Some(6)); |
| 33 | + assert_eq!(binary_to_decimal("1000011110"), Some(542)); |
| 34 | + assert_eq!(binary_to_decimal("1111111111"), Some(1023)); |
| 35 | + } |
| 36 | + #[test] |
| 37 | + fn big_binary_to_decimal() { |
| 38 | + assert_eq!( |
| 39 | + binary_to_decimal("111111111111111111111111"), |
| 40 | + Some(16_777_215) |
| 41 | + ); |
| 42 | + // 32 bits |
| 43 | + assert_eq!( |
| 44 | + binary_to_decimal("11111111111111111111111111111111"), |
| 45 | + Some(4_294_967_295) |
| 46 | + ); |
| 47 | + // 64 bits |
| 48 | + assert_eq!( |
| 49 | + binary_to_decimal("1111111111111111111111111111111111111111111111111111111111111111"), |
| 50 | + Some(18_446_744_073_709_551_615u128) |
| 51 | + ); |
| 52 | + } |
| 53 | + #[test] |
| 54 | + fn very_big_binary_to_decimal() { |
| 55 | + // 96 bits |
| 56 | + assert_eq!( |
| 57 | + binary_to_decimal( |
| 58 | + "1111111111111111111111111111111111111111111111111111111111111111\ |
| 59 | + 11111111111111111111111111111111" |
| 60 | + ), |
| 61 | + Some(79_228_162_514_264_337_593_543_950_335u128) |
| 62 | + ); |
| 63 | + |
| 64 | + // 128 bits |
| 65 | + assert_eq!( |
| 66 | + binary_to_decimal( |
| 67 | + "1111111111111111111111111111111111111111111111111111111111111111\ |
| 68 | + 1111111111111111111111111111111111111111111111111111111111111111" |
| 69 | + ), |
| 70 | + Some(340_282_366_920_938_463_463_374_607_431_768_211_455u128) |
| 71 | + ); |
| 72 | + // 129 bits, should overflow |
| 73 | + assert!(binary_to_decimal( |
| 74 | + "1111111111111111111111111111111111111111111111111111111111111111\ |
| 75 | + 11111111111111111111111111111111111111111111111111111111111111111" |
| 76 | + ) |
| 77 | + .is_none()); |
| 78 | + // obviously none |
| 79 | + assert!(binary_to_decimal( |
| 80 | + "1111111111111111111111111111111111111111111111111111111111111111\ |
| 81 | + 1111111111111111111111111111111111111111111111111111111111111\ |
| 82 | + 1111111111111111111111111111111111111111111111111111111111111\ |
| 83 | + 1111111111111111111111111111111111111111111111111111111111111\ |
| 84 | + 1111111111111111111111111111111111111111111111111111111111111\ |
| 85 | + 1111111111111111111111111111111111111111111111111111111111111\ |
| 86 | + 1111111111111111111111111111111111111111111111111111111111111" |
| 87 | + ) |
| 88 | + .is_none()); |
| 89 | + } |
| 90 | +} |
0 commit comments