Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the bitwise NOT operator in Rust?

Tags:

operators

rust

Looking at the list of bitwise operators in the Rust Book, I don't see a NOT operator (like ~ in C). Is there no NOT operator in Rust?

like image 662
Verax Avatar asked Aug 11 '16 12:08

Verax


People also ask

What is bitwise Not operator?

The bitwise NOT operator in C++ is the tilde character ~ . Unlike & and |, the bitwise NOT operator is applied to a single operand to its right. Bitwise NOT changes each bit to its opposite: 0 becomes 1, and 1 becomes 0.

What is == in Rust?

var = expr , ident = type. Assignment/equivalence. == expr == expr. Equality comparison.

What is Bitshift?

Bitshifting shifts the binary representation of each pixel to the left or to the right by a pre-defined number of positions. Shifting a binary number by one bit is equivalent to multiplying (when shifting to the left) or dividing (when shifting to the right) the number by 2.


1 Answers

The ! operator is implemented for many primitive types and it's equivalent to the ~ operator in C. See this example (playground):

let x = 0b10101010u8; let y = !x; println!("x: {:0>8b}", x); println!("y: {:0>8b}", y); 

Outputs:

x: 10101010 y: 01010101 

See also:

  • How do you set, clear and toggle a single bit in Rust?
like image 145
Lukas Kalbertodt Avatar answered Sep 22 '22 07:09

Lukas Kalbertodt