Bitwise Operations: Checking if x XOR y = 0

  • MHB
  • Thread starter mathmari
  • Start date
  • Tags
    Operations
In summary, Evgeny is trying to check if x XOR y=0 using bitwise operations, but is having trouble understanding the operation. He asks for someone else's help, and is given a summary of the problem. He then implements a command to help him with the problem, and ends the summary with a question.
  • #1
mathmari
Gold Member
MHB
5,049
7
Hey! :eek:

I want to check using bitwise operations if x XOR y=0.

The only conditional command we have is JMN a, which means Jump to a if the currect value is <0.

So, we have to check if it doesn't hold that x XOR y <0 and x XOR y >0, right? (Wondering)

But how can we check the condition x XOR y >0 ? (Wondering)

Using the operation NOT ( x XOR y ) we don't get -(x XOR y) right? (Wondering)
 
Physics news on Phys.org
  • #2
mathmari said:
I want to check using bitwise operations if x XOR y=0.
What do you mean by checking? What are x and y?

mathmari said:
The only conditional command we have is JMN a, which means Jump to a if the currect value is <0.
And literally no single other command?
 
  • #3
mathmari said:
Using the operation NOT ( x XOR y ) we don't get -(x XOR y) right? (Wondering)

-(x XOR y) ? Do you mean $\lnot(x \ XOR \ y)$ ?

x XOR y will be 0 when either x or y is 1 but not both. You can see a truth table for this.

Evgeny makes a good point by asking what x and y are. Unless you are using some sort of thresholding operation, typically bitwise operations will have solutions of either true or false, 1 or 0, not 'greater than 0' or 'less than 0'.

Maybe you want to use some other variables to test the x XOR y condition, and then assign something to a data type that can handle the '> 0' part?
 
  • #4
Joppy said:
x XOR y will be 0 when either x or y is 1 but not both. You can see a truth table for this.

XOR checks if each bit of the two values is the same, or not?
I mean that if one bit is the same the result is 0, and if not the result is 1, right? (Wondering)

For example, we have that [m]1100 XOR 1010 = 0110[/m]. So, only if the result is 0 we have that the two numbers are the same, right? (Wondering)
It holds that $x-y=0$ if it doesn't hold that $x-y<0$ and neither $x-y>0 \Rightarrow -(x-y)<0$.

I implemented now the command NEG, that computes the negation of a constant.

It holds that $x+ \sim x+1=0$. Therefore it holds that $-x= \sim x +1$.
($\sim x$ is the bitwise negation of $x$)
So, for two constants $x$ and $y$, we want to check if $x \ XOR \ y=0$.

1) We compute the [m]x XOR y[/m]
2) If the result is negative (JMN) then go to step 6
3) We compute the negation of the above result
4) If this is negative (JMN) then go to step 6
5) It holds that $x \ XOR \ y=0$ and so $x=y$
6) It holds that $x \ XOR \ y\neq 0$ and so $x\neq y$ Is everything correct? (Wondering)
 
  • #5
mathmari said:
XOR checks if each bit of the two values is the same, or not?

I mean that if one bit is the same the result is 0, and if not the result is 1, right? (Wondering)

It's just an operator. But i guess we can say it 'checks'. If two of the bits are the same, then the result is false. For all other cases, the result is true.
For example, we have that [m]1100 XOR 1010 = 0110[/m]. So, only if the result is 0 we have that the two numbers are the same, right? (Wondering)

Hmm. No? $1100 \ne 1010$ in base 10 and base 2. I see what you are trying to do though.. Can't we just,

Code:
if((x - y) == 0) {... bla ...}
     else if {... bla ...}

Or something along those lines :)I feel like you might be applying the rules and conditions of ordinary algebra to that of Boolean algebra. Changing the data type wherever it suits, so I don't really understand. I will leave it for someone more knowledgeable in the area to chime in :).
 
  • #6
My two cents as someone who is pretty rusty in assembly language: I think the answer depends entirely on the instruction set of your assembly language, and depending on the commands thereof, there may be multiple ways of tackling this problem. Some assembly languages also have special registers that have particular functions which can simplify code.

I think an important question to ask is which assembly language /processor you are using and which commands are available to you. The processor I used in the past had an operation XOR that would perform bitwise exclusive-or on two binary variables, and if resulted in 0, would toggle a certain bit on a register. All that was left to do was to check if that bit was set or not, and then branch from there. Sometimes you may be able to do this task directly if you have a "comparison" operator followed by a jump instruction.

For example:

CMP X, Y
JMN LESSTHAN
GREATERTHAN:
;do something
LESSTHAN:
;do something

If I recall correctly, the compare operator does perform some sort of subtraction, so the logic basically reduces to what Joppy has said. If you don't have a CMP operator, you may be able to use a SUB operator instead.
 

FAQ: Bitwise Operations: Checking if x XOR y = 0

What is a bitwise operation?

A bitwise operation is a type of mathematical operation that is performed on individual bits in a binary number. These operations are useful for manipulating and comparing binary data, such as in computer programming.

What does XOR mean?

XOR (exclusive OR) is a logical operator that compares two bits and returns a 1 if they are different, and a 0 if they are the same. In other words, XOR checks for inequality between two bits.

How does XOR relate to checking if x XOR y = 0?

When x XOR y = 0, it means that the two binary numbers have an equal number of 1s and 0s in the same positions. This is because XOR only returns a 1 when the two bits being compared are different. So, if x and y have an equal number of 1s and 0s, their XOR result will be 0. This property can be used to check if two binary numbers are equal.

What is the purpose of checking if x XOR y = 0?

Checking if x XOR y = 0 can be useful in certain applications, such as error detection and data validation. It can also be used to compare two binary numbers for equality, as mentioned before. Additionally, it can be used in encryption algorithms to scramble data in a specific way.

Are there other bitwise operations that can be used to check for equality?

Yes, there are other bitwise operations that can be used to check for equality, such as AND and OR. However, XOR is particularly useful because it only returns a 1 when the two bits being compared are different, making it more efficient for certain applications.

Similar threads

Replies
1
Views
1K
Replies
4
Views
1K
Replies
2
Views
2K
Replies
3
Views
1K
Replies
5
Views
2K
Replies
2
Views
1K
Replies
1
Views
1K
Replies
5
Views
2K
Back
Top