Back
Close

Rust for Python Developers - Unsigned, Signed Integers and Casting

Shin_O
2,853 views

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.

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.

Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants