What is python’s " "?

Asked

Viewed 2,609 times

1

I was seeing what it is, ta speaking "xor", but if I give 5 11 results in 14 and 5 5, 0? What exactly is this result?

3 answers

5

Kaigogames,

imagine the following the number 5 (decimal) is represented by 0101 (binary) and the number 11 (decimal) is represented by 1011 (binary) soon if an operation is being performed XOR you will have the result of 14 (decimal) which is represented by 1110 (binary).

  • thank you, got it

4


Before we look at this, it’s good before we study operators bit by bit. Bit-by-bit operators are forms of operators as well as +, -, * or / sane arithmetic operators.

To begin with, we know that the computer operates only with binary values (or 0 or 1), that instead of using our decimal basis, which has 10 possible numbers at each position of a digit in a number, it uses base 2. All when it is information that you can imagine is going through your processor now, it is being processed in two different possibilities.

Python, as a software that operates under this computer, works the same way. That’s where these operators come in.

Binary system

We say base, because we can actually represent this number in the form of power. For example, the number 142 is the same as

(1 x 10 2) + (4 x 10 1) + (2 x 10 0)

[one times ten squared plus four times ten elevated to first plus two times ten to zero]

So we’re representing 10 as a power base. On the binary base, it works the same way:

What is the decimal value of 101?

1 x 2 2 = 4
0 x 2 1 = 0
1 x 2**0 = 1 (any number high to 0 is 1)
4 + 0 + 1 = 5

And the operators?

There are several operators who work with logic very different from each other, such that the main ones are &, and |. They spend bit by bit of that number processing according to their type.

First, I will deal with the operator of your question: the XOR operator.

The operator XOR, or OR EXCLUSIVE (^): the output of each bit is 1 if, and only if, a single bit of the input is 1.

EXAMPLE:

x = 7 # 111 em binário  
y = 5 # 101 em binário  
z = x ^ y # 010 em binário, ou 2 em decimal.

The operator OR, or OR (|): the output of each bit is 1 if at least one input bit is 1. It differs from XOR, because XOR requires it to be unique (or one, or the other, never both), while this is also valid if both are 1.

EXAMPLE:

x = 7 # 111 em binário  
y = 5 # 101 em binário  
z = x | y # 111 em binário, ou 7 em decimal.

The operator AND, or And (&): the output of each bit is 1 if, and only if, both bits have 1 value.

EXAMPLE:

x = 7 # 111 em binário  
y = 5 # 101 em binário  
z = x ^ y # 101 em binário, ou 5 em decimal.

-4

Operator-only bit OR: When the two binary bit corresponding different and the result is 1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.