Next: Type Functions, Previous: Time Functions, Up: Built-in [Contents][Index]

I can explain it for you, but I can’t understand it for you.

— *Anonymous*

Many languages provide the ability to perform *bitwise* operations
on two integer numbers. In other words, the operation is performed on
each successive pair of bits in the operands.
Three common operations are bitwise AND, OR, and XOR.
The operations are described in Table 9.6.

Bit Operator | AND | OR | XOR |—+—+—+—+—+— Operands | 0 | 1 | 0 | 1 | 0 | 1 ———-+—+—+—+—+—+— 0 | 0 0 | 0 1 | 0 1 1 | 0 1 | 1 1 | 1 0

As you can see, the result of an AND operation is 1 only when *both*
bits are 1.
The result of an OR operation is 1 if *either* bit is 1.
The result of an XOR operation is 1 if either bit is 1,
but not both.
The next operation is the *complement*; the complement of 1 is 0 and
the complement of 0 is 1. Thus, this operation “flips” all the bits
of a given value.

Finally, two other common operations are to shift the bits left or right.
For example, if you have a bit string ‘`10111001`’ and you shift it
right by three bits, you end up with ‘`00010111`’.^{58}
If you start over
again with ‘`10111001`’ and shift it left by three bits, you end up
with ‘`11001000`’.
`gawk`

provides built-in functions that implement the
bitwise operations just described. They are:

`and(`

`v1`,`v2`[, …])Return the bitwise AND of the arguments. There must be at least two.

`compl(`

`val`)Return the bitwise complement of

`val`.`lshift(`

`val`,`count`)Return the value of

`val`, shifted left by`count`bits.`or(`

`v1`,`v2`[, …])Return the bitwise OR of the arguments. There must be at least two.

`rshift(`

`val`,`count`)Return the value of

`val`, shifted right by`count`bits.`xor(`

`v1`,`v2`[, …])Return the bitwise XOR of the arguments. There must be at least two.

For all of these functions, first the double precision floating-point value is
converted to the widest C unsigned integer type, then the bitwise operation is
performed. If the result cannot be represented exactly as a C `double`

,
leading nonzero bits are removed one by one until it can be represented
exactly. The result is then converted back into a C `double`

. (If
you don’t understand this paragraph, don’t worry about it.)

Here is a user-defined function (see User-defined) that illustrates the use of these functions:

# bits2str --- turn a byte into readable 1's and 0's function bits2str(bits, data, mask) { if (bits == 0) return "0" mask = 1 for (; bits != 0; bits = rshift(bits, 1)) data = (and(bits, mask) ? "1" : "0") data while ((length(data) % 8) != 0) data = "0" data return data }

BEGIN { printf "123 = %s\n", bits2str(123) printf "0123 = %s\n", bits2str(0123) printf "0x99 = %s\n", bits2str(0x99) comp = compl(0x99) printf "compl(0x99) = %#x = %s\n", comp, bits2str(comp) shift = lshift(0x99, 2) printf "lshift(0x99, 2) = %#x = %s\n", shift, bits2str(shift) shift = rshift(0x99, 2) printf "rshift(0x99, 2) = %#x = %s\n", shift, bits2str(shift) }

This program produces the following output when run:

$gawk -f testbits.awk-| 123 = 01111011 -| 0123 = 01010011 -| 0x99 = 10011001 -| compl(0x99) = 0xffffff66 = 11111111111111111111111101100110 -| lshift(0x99, 2) = 0x264 = 0000001001100100 -| rshift(0x99, 2) = 0x26 = 00100110

The `bits2str()`

function turns a binary number into a string.
The number `1`

represents a binary value where the rightmost bit
is set to 1. Using this mask,
the function repeatedly checks the rightmost bit.
ANDing the mask with the value indicates whether the
rightmost bit is 1 or not. If so, a `"1"`

is concatenated onto the front
of the string.
Otherwise, a `"0"`

is added.
The value is then shifted right by one bit and the loop continues
until there are no more 1 bits.

If the initial value is zero it returns a simple `"0"`

.
Otherwise, at the end, it pads the value with zeros to represent multiples
of 8-bit quantities. This is typical in modern computers.

The main code in the `BEGIN`

rule shows the difference between the
decimal and octal values for the same numbers
(see Nondecimal-numbers),
and then demonstrates the
results of the `compl()`

, `lshift()`

, and `rshift()`

functions.

This example
shows that 0’s come in on the left side. For `gawk`

, this is
always true, but in some languages, it’s possible to have the left side
fill with 1’s. Caveat emptor.

Next: Type Functions, Previous: Time Functions, Up: Built-in [Contents][Index]