O'Reilly logo

Learning AWK Programming by Shiwang Kalkhanda

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The xor (num1, num2) function

This function returns the result of a bitwise XOR operation on arguments. There must be at least two arguments. Its syntax is as follows:

xor(num1, num2, [, ...])

In the XOR operation, for the output to be 1, both of the input bits should be different—that is, one of the bits should be 1. When both bits are the same, XOR returns 0. The following truth table summarizes how the XOR operation works when processing two bits:

0 xor 0 = 0

0 xor 1 = 1

1 xor 0 = 1

1 xor 1 = 0

The following XOR operation on decimal 5 and 6 illustrates the working of the xor() function.

5 = 101

6 = 110

5 and 6 = 011 which is decimal 3

The following example shows how the xor() functions work:

$ vi xor.awkBEGIN    {    num1 = 5    num2 = 6 result ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required