AWK has the following built-in bit manipulation functions −
Performs bitwise AND operation.
[jerry]$ awk 'BEGIN { num1 = 10 num2 = 6 printf "(%d AND %d) = %d\n", num1, num2, and(num1, num2) }'
On executing this code, you get the following result −
(10 AND 6) = 2
It performs bitwise COMPLEMENT operation.
[jerry]$ awk 'BEGIN { num1 = 10 printf "compl(%d) = %d\n", num1, compl(num1) }'
On executing this code, you get the following result −
compl(10) = 9007199254740981
It performs bitwise LEFT SHIFT operation.
[jerry]$ awk 'BEGIN { num1 = 10 printf "lshift(%d) by 1 = %d\n", num1, lshift(num1, 1) }'
On executing this code, you get the following result −
lshift(10) by 1 = 20
It performs bitwise RIGHT SHIFT operation.
[jerry]$ awk 'BEGIN { num1 = 10 printf "rshift(%d) by 1 = %d\n", num1, rshift(num1, 1) }'
On executing this code, you get the following result −
rshift(10) by 1 = 5
It performs bitwise OR operation.
[jerry]$ awk 'BEGIN { num1 = 10 num2 = 6 printf "(%d OR %d) = %d\n", num1, num2, or(num1, num2) }'
On executing this code, you get the following result −
(10 OR 6) = 14
It performs bitwise XOR operation.
[jerry]$ awk 'BEGIN { num1 = 10 num2 = 6 printf "(%d XOR %d) = %d\n", num1, num2, xor(num1, num2) }'
On executing this code, you get the following result −
(10 bitwise xor 6) = 12