Sitemap

Tilde (~) operator

1 min readApr 17, 2020

--

The tilde operator is used to find the complement of a binary number.

Complement of a binary number can be:

→ 1’s complement: flip all bits (0 to 1 and 1 to 0). e.g: 12(1100) → 3(0011)

Get Sayak Chattopadhyay’s stories in your inbox

Join Medium for free to get updates from this writer.

→ 2’s complement: 1 added to the 1’s complement. e.g: 12(1100) →4(0100)

The tilde operator does bitwise inversion of a integer number which means bitwise inversion of x is -(x+1)

0 → -1

1 → -2

--

--