Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
This story was originally published on Medium
Follow me:
Bitwise Negation
Rust uses !
for the Bitwise Negation (Bitwise NOT). This produces different results depends on the type.
1
2
3
4
5
6
7
8
9
10
11
12
13
fn main() {
let i:i32;
let u:u32;
i = 2;
u = 2;
println!("u is {}", u);
println!("u in binary is {:b}", u);
println!("i is {}", i);
println!("i in binary is {:b}", i);
println!("Bitwise negation !u is {}", !u);
println!("Bitwise negation !i is {}", !i);
}
Enter to Rename, Shift+Enter to Preview
As you see !2
with the unsigned type returns 4294967293
and with the signed type returns -3
.
The bitwise negation on a signed integer returns the two’s complement as we saw previously in Rust signed two’s complement integer types.
Adding a Negative Number
The subtraction is the same as adding a negative number.
5 - 2 = 5 + (-2) = 3
This applies to binary as well.
0101 - 0010
= 0101 + (-0010) // (1)
= 0101 + 1110 // (2)
= 0011 // this is 3 in decimal number.
We find -0010
by finding the two’s complement of 0010
that is 1110
.
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content