1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Understanding bitwise NOT in python

Discussão em 'Python' iniciado por Stack, Setembro 28, 2024 às 12:43.

  1. Stack

    Stack Membro Participativo

    I was trying to understand bitwise NOT in python.

    I tried following:

    print('{:b}'.format(~ 0b0101))
    print(~ 0b0101)


    The output is

    -110
    -6


    I tried to understand the output as follows:

    Bitwise negating 0101 gives 1010. With 1 in most significant bit, python interprets it as a negative number in 2's complement form and to get back corresponding decimal it further takes 2's complement of 1010 as follows:

    1010
    0101 (negating)
    0110 (adding 1 to get final value)


    So it prints it as -110 which is equivalent to -6.

    Am I right with this interpretation?

    Continue reading...

Compartilhe esta Página