XOR - operator usage on vectors with numerical elements in it.

considering the below 2 vectors
x <- c(3, 4, 5)
y <- c(3, 5, 1)

output
x & y # TRUE TRUE TRUE

For bool,
XOR(TRUE,FALSE) => False

However, In the former example mentioned, unable to figure out what is the logic used while applying XOR on numbers eg: for XOR(3,3) or XOR(4,5) why does it return False False False? Need help.

What??? That result is TRUE!

xor(TRUE, FALSE)
#> [1] TRUE

Created on 2022-10-17 by the reprex package (v2.0.1)

The numbers get coerced to logical (boolean) before applying any boolean operation. So 0 -> FALSE; any other number -> TRUE. This is made obvious by forcing the coercion:

as.logical(c(3,4,5,0,-1))
#> [1]  TRUE  TRUE  TRUE FALSE  TRUE

Created on 2022-10-17 by the reprex package (v2.0.1)

So in your example, 3,4,5,1 are all versions of TRUE, so of course using & gives you TRUE.

Thank you so much. That was really helpful.

Yes, my bad.
XOR(TRUE,FALSE) => True

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.