bitFlip: Binary Flip (Not) Operator

Description Usage Arguments Value Note Author(s) See Also Examples

View source: R/bitops.R

Description

The binary flip (‘not’, R's !) operator, bitFlip(a, w), “flips every bit” of a up to the w-th bit.

Usage

1
bitFlip(a, bitWidth = 32)

Arguments

a

numeric vector.

bitWidth

scalar integer between 0 and 32.

Value

(“binary”) numeric vector of the same length as a masked with (2^bitWidth)-1. NA is returned for any value of a that is not finite or whose magnitude is greater or equal to 2^{32}.

Note

bitFlip(a, w) is an “involution”, i.e. it is its own inverse – when a is in \{0, 1, .., 2^{32}-1\}. Notably, negative values a are equivalent to their values in the above range, see also bitUnique() in the ‘Examples’.

Author(s)

Steve Dutky

See Also

bitShiftL, bitXor, etc.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
bitFlip(0:5)
##
bitUnique <- function(x) bitFlip(bitFlip(x)) # "identity" when x in  0:(2^32-1)
bitUnique(  0:16 ) # identical (well, double precision)
bitUnique(-(1:16)) # 4294967295 ...
stopifnot(
  identical(bitUnique(-(1:16)), 2^32 -(1:16)),
  bitFlip(-1) == 0,
  bitFlip(0 ) == 2^32 - 1,
  bitFlip(0, bitWidth=8) == 255
)

bitops documentation built on April 24, 2021, 5:07 p.m.