live-de-python icon indicating copy to clipboard operation
live-de-python copied to clipboard

[SUGESTÃO] Live: Operadores Lógicos Booleanos em Python

Open d3cryptofc opened this issue 3 years ago • 1 comments

AND, && (E) Verdadeiro se as duas entradas forem verdadeiras, caso contrário falso.

1 and 1 # 1
0 and 1 # 0
1 and 0 # 0
0 and 0 # 0

OR, || (OU) Verdadeiro se pelo menos uma das entradas forem verdadeiras, caso contrário falso.

1 or 1 # 1
0 or 1 # 1
1 or 0 # 1
0 or 0 # 0

NOT, ! (NÃO) Inverte o valor de entrada.

not 0 # 1
not 1 # 0

XOR, ^ (Exclusive OR | OU Exclusivo) Verdadeiro apenas se as duas entradas forem diferentes, caso contrário falso.

1 ^ 1 # 0
0 ^ 1 # 1
1 ^ 0 # 1
0 ^ 0 # 0

NAND (Not AND | Não AND) Apenas a inversão do AND.

not (1 and 1) # 0
not (0 and 1) # 1
not (1 and 0) # 1
not (0 and 0) # 1

NOR (Not OR | Não OR) Apenas a inversão do OR.

not (1 or 1) # 0
not (0 or 1) # 0
not (1 or 0) # 0
not (0 or 0) # 1

XNOR (Not Exclusive OR) Apenas a inversão do XOR.

not (1 ^ 1) # 1
not (0 ^ 1) # 0
not (1 ^ 0) # 0
not (0 ^ 0) # 1

É um tema que dá grandes poderes, acho que vale a pena..

d3cryptofc avatar Sep 06 '22 17:09 d3cryptofc

Se caso for um tema muito curto, seria interessante acrescentar os operadores de bitwise.

Operadores de bitwise são chamados de operadores bit-a-bit, significa que ao passar 2 bytes ele irá comparar os bits dos dois bytes usando determinado operador, retornando um novo byte.

Bitwise XOR (^)

1 ^ 2 # 3

bin(1) # '0b1'  -> 0000 0001
bin(2) # '0b10' -> 0000 0010
bin(3) # '0b11' -> 0000 0011

Bitwise OR (|)

1 | 2 # 3

bin(1) # '0b1'  -> 0000 0001
bin(2) # '0b10' -> 0000 0010
bin(3) # '0b11' -> 0000 0011

Bitwise (AND) (&)

1 & 2 # 0

bin(1) # '0b1'  -> 0000 0001
bin(2) # '0b10' -> 0000 0010
bin(0) # '0b0'  -> 0000 0000

Bitwise NOT (~) - Inverte os bits de um byte

~1 # -2

bin(1) # '0b1'   -> 0000 0001
bin(2) # '-0b10' -> 0000 0010

Left Shift (<<) - Move os bits X casas para a esquerda.

5 << 2 # 20
bin(5)  # '0b101'    -> 0000 0101
bin(20) # '0b10100'  -> 0001 0100

Right Shift (>>) - Move os bits X casas para a direita.

5 >> 2 # 1
bin(5) # '0b101' -> 0000 0101
bin(1) # '0b01'  -> 0000 0001

d3cryptofc avatar Sep 09 '22 13:09 d3cryptofc