AWK - Bit Manipulation Functions



AWK has the following built-in bit manipulation functions −

and

Performs bitwise AND operation.

Example

[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 −

Output

(10 AND 6) = 2

compl

It performs bitwise COMPLEMENT operation.

Example

[jerry]$ awk 'BEGIN {
   num1 = 10
   printf "compl(%d) = %d\n", num1, compl(num1)
}'

On executing this code, you get the following result −

Output

compl(10) = 9007199254740981

lshift

It performs bitwise LEFT SHIFT operation.

Example

[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 −

Output

lshift(10) by 1 = 20

rshift

It performs bitwise RIGHT SHIFT operation.

Example

[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 −

Output

rshift(10) by 1 = 5

or

It performs bitwise OR operation.

Example

[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 −

Output

(10 OR 6) = 14

xor

It performs bitwise XOR operation.

Example

[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 −

Output

(10 bitwise xor 6) = 12
awk_built_in_functions.htm
Advertisements