File tree Expand file tree Collapse file tree 3 files changed +30
-0
lines changed Expand file tree Collapse file tree 3 files changed +30
-0
lines changed Original file line number Diff line number Diff line change
1
+ pub fn decimal_to_binary ( base_num : u64 ) -> String {
2
+ let mut num = base_num;
3
+ let mut binary_num = String :: new ( ) ;
4
+ loop {
5
+ let bit = ( num % 2 ) . to_string ( ) ;
6
+ binary_num. push_str ( & bit) ;
7
+ num /= 2 ;
8
+ if num == 0 {
9
+ break ;
10
+ }
11
+ }
12
+
13
+ let bits = binary_num. chars ( ) ;
14
+ bits. rev ( ) . collect ( )
15
+ }
16
+
17
+ #[ cfg( test) ]
18
+ mod tests {
19
+ use super :: * ;
20
+
21
+ #[ test]
22
+ fn converting_decimal_to_binary ( ) {
23
+ assert_eq ! ( decimal_to_binary( 542 ) , "1000011110" ) ;
24
+ assert_eq ! ( decimal_to_binary( 92 ) , "1011100" ) ;
25
+ }
26
+ }
Original file line number Diff line number Diff line change
1
+ mod decimal_to_binary;
2
+
3
+ pub use self :: decimal_to_binary:: decimal_to_binary;
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ pub mod backtracking;
4
4
pub mod big_integer;
5
5
pub mod ciphers;
6
6
pub mod compression;
7
+ pub mod conversions;
7
8
pub mod data_structures;
8
9
pub mod dynamic_programming;
9
10
pub mod general;
You can’t perform that action at this time.
0 commit comments