-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_representation.rs
124 lines (101 loc) · 3.52 KB
/
binary_representation.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Declare BinaryRepresentation struct
pub struct BinaryRepresentation {
pub(crate) arr: Vec<bool>,
}
// Implement the previously declared BinaryRepresentation struct
impl BinaryRepresentation {
// Returns a new BinaryRepresentation object
pub fn new() -> BinaryRepresentation {
return BinaryRepresentation { arr: Vec::new() };
}
// Creates and returns a new BinaryRepresentation object from the provided vec
pub fn from_vec(vec: Vec<bool>) -> BinaryRepresentation {
return BinaryRepresentation { arr: vec };
}
// Creates and returns a new BinaryRepresentation object from the provided number
pub fn from_num(num: i32) -> BinaryRepresentation {
let mut bin_rep = BinaryRepresentation::new();
bin_rep.set_number(num);
return bin_rep;
}
// Convert the provided number to binary and set the vec of
// this representation to the converted binary
pub fn set_number(&mut self, num: i32) {
self.arr.clear();
let mut temp_var: i32;
let mut quotient = num / 2;
let mut remainder = num % 2;
// Push the first bit
self.arr.push(if remainder == 0 { false } else { true });
// Loop
while quotient > 0 {
temp_var = quotient;
quotient = temp_var / 2;
remainder = temp_var % 2;
// Push the calculated bit
self.arr.push(if remainder == 0 { false } else { true });
}
// Reverse the result
self.reverse();
}
// Zero-pads the vec of this representation
// If the length of the vec of this representation is greater than the
// provided length nothing will happen
pub fn transform(&mut self, len: usize) {
if self.len() >= len {
// Abort
return;
}
let mut vec: Vec<bool> = Vec::new();
let mut diff = len - self.len();
while diff > 0 {
// Push false (0) while the difference is greater than 0
vec.push(false);
diff -= 1;
}
// Append the vec of this representation to the zero filled vec and overwrite
// the vec of this representation with the new vec
vec.append(&mut self.arr);
self.arr = vec;
}
// Reverse this representation
pub fn reverse(&mut self) {
self.arr.reverse()
}
// Convert this representation to a string
pub fn to_string(&self) -> String {
let mut str = String::new();
for x in &self.arr {
str.push(if *x { '1' } else { '0' });
}
return str;
}
// Convert this representation to a decimal
pub fn to_i32(&self) -> i32 {
let len = self.len();
let mut decimal = 0;
for i in 0..len {
let bit = if self.arr[i] { 1 } else { 0 };
// I don't really know what this calculation does but the formula says
// this is right and it works so I'm not complaining
decimal += ((bit as f32) * (2 as f32).powi((len - 1 - i) as i32)) as i32;
}
return decimal;
}
// Calculate the amount of bits of num in binary
fn calc_bits(num: &i32) -> i32 {
let mut temp_var: i32;
let mut idx = 0;
let mut quotient = num / 2;
while quotient > 0 {
temp_var = quotient;
quotient = temp_var / 2;
idx += 1;
}
return idx + 1;
}
// Return the amount of bits of this representation
pub fn len(&self) -> usize {
return self.arr.len();
}
}