MHB Bitwise Operations: Checking if x XOR y = 0

  • Thread starter Thread starter mathmari
  • Start date Start date
  • Tags Tags
    Operations
AI Thread Summary
The discussion focuses on using bitwise operations, specifically XOR, to determine if two values x and y are equal by checking if x XOR y equals 0. Participants clarify that XOR results in 0 when both bits are the same, and they explore how to implement this check using limited assembly commands, particularly the JMN (Jump if Negative) command. There is debate about the necessity of additional variables or commands to handle comparisons beyond simple bitwise operations. The importance of understanding the specific assembly language and its available commands is emphasized, as this affects how the problem can be approached. Ultimately, the conversation highlights the complexity of implementing logical checks in assembly language with limited operations.
mathmari
Gold Member
MHB
Messages
4,984
Reaction score
7
Hey! :o

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
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?
 
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?
 
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)
 
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 :).
 
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.
 
I was reading documentation about the soundness and completeness of logic formal systems. Consider the following $$\vdash_S \phi$$ where ##S## is the proof-system making part the formal system and ##\phi## is a wff (well formed formula) of the formal language. Note the blank on left of the turnstile symbol ##\vdash_S##, as far as I can tell it actually represents the empty set. So what does it mean ? I guess it actually means ##\phi## is a theorem of the formal system, i.e. there is a...

Similar threads

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