Bit-by-bit operations (XOR)

Asked

Viewed 117 times

2

#include <stdio.h>


void main(){

    int a = 356, b = 3;
    printf("%d\n", a ^ b); /*


system("pause");
}

Somebody explain to me why this xor does 359? The conversion to binary gets:

101100100 --> 356
11 --> 3

How do I compare these 2 values? To be positive only one of the entries can be positive correct?

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site

1 answer

3

The xor It’s just a sum without the "go one":

101100100 -> 356
000000011 -> 3
---------
101100111 -> 359

Whenever I find 0 and 0 is 0, whenever I find 0 and 1 or 1 and 0 is 1 and whenever I find 1 and 1 is 0.

So the bit comparison depends on the exact way it’s treated in the specific architecture, those bits up there don’t seem to consider signal. But the xor operates exactly as in mathematics if both are positive (0) then it is positive, if both are negative (1) it is positive (0) and if different it is negative (1).

All mathematical operations on the computer take place in the bits, the way each operation is done changes. And in fact all computer operations are done through logical ports and, or and not. There are even some optimized ports that can do something a little more sophisticated, but not much, so there’s a door xor also simplifies some operation and no longer needs a port (on very simple architectures to have a xor needs more than one door).

I want to emphasize that this is not usually true inside a computer, this is an abstract form that we use to better understand what is happening, including because each architecture does it differently.

Although everything is done in the bits by the computer, this is not something you usually need to know unless you are actually operating on bits that happen to be on the same data. You work in numbers. There are no binary, decimal or hexadecimal numbers, as some think there are, there are only numbers (no texts exist, or other information, only numbers, but we don’t all see them as numbers). Almost always a human wants to know see numbers in decimal form, and computers at all times operate on the bits. Contrary to what some think there is no conversion to work with one way or another, there is conversion when you enter a data into the computer (they saw bits) and when they will be shown in some way for a person to see (usually will be shown as decimal, but can show in many other ways), but numerical counting units exist in the world without looking at the way it is written or represented.

Here I show you another way to indicate the number you know as three (oops, I just wrote another way):

3 patos de borracha

  • These logic operators like XOR tbm work with hexadecimal ? or only binary ?

  • 1

    It works with numbers, bits are just an abstract way of understanding, there are no binary numbers, hexadecimals, decimals, how you think they exist, there is only number.

Browser other questions tagged

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