educative.io

Bitwise complement

i don’t get how you get a complement of a number at all. ~12 returns -13, why? 12 in binary is 1100, then 0011, which is 3?

maybe this is related to the fact that there are 8 digits, then why is a binary of the numbers 8 digits?


Type your question above this line.

Course: https://www.educative.io/collection/10370001/4974430444847104
Lesson: https://www.educative.io/collection/page/10370001/4974430444847104/4679767066411008

Hi @Yo_Sato,

Why ~12 is -13 has to do with how the numbers are represented bitwise. Numbers are represented as 2’s complement. So 12 has the binary value 0000 1100. ~12 flips the bits and the binary value becomes 1111 0011 which is the binary representation of the -13.

Yes, we’re using 8 digit binary number as you can see in the illustration of the lesson.