|
|
|
@ -258,27 +258,33 @@ Rust 的位运算基本上和其他语言一样
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
fn main() {
|
|
|
|
|
// 二进制为00000010
|
|
|
|
|
let a:i32 = 2;
|
|
|
|
|
// 无符号8位整数,二进制为00000010
|
|
|
|
|
let a: u8 = 2; // 也可以写 let a: u8 = 0b_0000_0010;
|
|
|
|
|
|
|
|
|
|
// 二进制为00000011
|
|
|
|
|
let b:i32 = 3;
|
|
|
|
|
let b: u8 = 3;
|
|
|
|
|
|
|
|
|
|
// {:08b}:左高右低输出二进制01,不足8位则高位补0
|
|
|
|
|
println!("a value is {:08b}", a);
|
|
|
|
|
|
|
|
|
|
println!("b value is {:08b}", b);
|
|
|
|
|
|
|
|
|
|
println!("(a & b) value is {}", a & b);
|
|
|
|
|
println!("(a & b) value is {:08b}", a & b);
|
|
|
|
|
|
|
|
|
|
println!("(a | b) value is {}", a | b);
|
|
|
|
|
println!("(a | b) value is {:08b}", a | b);
|
|
|
|
|
|
|
|
|
|
println!("(a ^ b) value is {}", a ^ b);
|
|
|
|
|
println!("(a ^ b) value is {:08b}", a ^ b);
|
|
|
|
|
|
|
|
|
|
println!("(!b) value is {} ", !b);
|
|
|
|
|
println!("(!b) value is {:08b}", !b);
|
|
|
|
|
|
|
|
|
|
println!("(a << b) value is {}", a << b);
|
|
|
|
|
println!("(a << b) value is {:08b}", a << b);
|
|
|
|
|
|
|
|
|
|
println!("(a >> b) value is {}", a >> b);
|
|
|
|
|
println!("(a >> b) value is {:08b}", a >> b);
|
|
|
|
|
|
|
|
|
|
let mut a = a;
|
|
|
|
|
// 注意这些计算符除了!之外都可以加上=进行赋值 (因为!=要用来判断不等于)
|
|
|
|
|
a <<= b;
|
|
|
|
|
println!("(a << b) value is {}", a);
|
|
|
|
|
println!("(a << b) value is {:08b}", a);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|