l8kyu/bits.rs
1//!
2//! Modules categorized by Codewars labels - 8kyu *** Bits ***
3//!
4
5use shared::kata::*;
6
7/// You are given a 32-bit float (single precision).
8/// Return the number a signed 32-bit integer would hold
9/// with the same bit pattern.
10/// # Example
11/// ``` 10.0 (f32) == 01000001001000000000000000000000 (binary) ``` <br>
12/// returns 1092616192 (i32)
13pub fn convert_to_i32(f: f32) -> i32 {
14 let kata = Kata {
15 level: Level::L8kyu,
16 tags: vec![Tag::Bits, Tag::Fundamentals],
17 description: String::from("Casting binary float to integer"),
18 };
19
20 println!(
21 "Level: {:?}, Tags: {:?}, Description: {}",
22 kata.level, kata.tags, kata.description
23 );
24
25 // unsafe { std::mem::transmute(f) } option 1
26 f.to_bits() as i32 // option 2
27}