R: Bitwise Logical Operations

bitwise {base} | R Documentation

## Bitwise Logical Operations

### Description

Logical operations on integer vectors with elements viewed as sets of bits.

### Usage

    bitwNot(a)
    bitwAnd(a, b)
    bitwOr(a, b)
    bitwXor(a, b)

    bitwShiftL(a, n)
    bitwShiftR(a, n)

### Arguments

`a, b` | integer vectors; numeric vectors are coerced to integer vectors.
`n` | non-negative integer vector of values up to 31.

### Details

Each element of an integer vector has 32 bits.

Pairwise operations can result in integer `NA`.

Shifting is done assuming the values represent unsigned integers.

### Value

An integer vector of length the longer of the arguments, or zero length if one is zero-length.

The output element is `NA` if an input is `NA` (after coercion) or an invalid shift.

### See Also

The logical operators, `!`, `&`, `|`, `xor`. Notably these do work bitwise for `raw` arguments.

The classes `"octmode"` and `"hexmode"` whose implementation of the standard logical operators is based on these functions.

Package bitops has similar functions for numeric vectors which differ in the way they treat integers 2^31 or larger.

### Examples

    bitwNot(0:12) # -1 -2  ... -13
    bitwAnd(15L, 7L) #  7
    bitwOr (15L, 7L) # 15
    bitwXor(15L, 7L) #  8
    bitwXor(-1L, 1L) # -2

    ## The "same" for 'raw' instead of integer :
    rr12 <- as.raw(0:12) ; rbind(rr12, !rr12)
    c(r15 <- as.raw(15), r7 <- as.raw(7)) #  0f 07
    r15 & r7    # 07
    r15 | r7    # 0f
    xor(r15, r7)# 08

    bitwShiftR(-1, 1:31) # shifts of 2^32-1 = 4294967295

[Package base version 3.5.1 Index]