Query the number of bits in a bit() vector or change the number of bits in a bit vector. Query the number of bits in a bitwhich()] vector or change the number of bits in a bit vector.

# S3 method for class 'bit'
length(x)

# S3 method for class 'bit'
length(x) <- value

# S3 method for class 'bitwhich'
length(x)

# S3 method for class 'bitwhich'
length(x) <- value

# S3 method for class 'ri'
length(x)

Arguments

x

a bit(), bitwhich() or ri() object

value

the new number of bits

Value

the length A bit vector with the new length

Details

NOTE that the length does NOT reflect the number of selected (TRUE) bits, it reflects the sum of both, TRUE and FALSE bits. Increasing the length of a bit() object will set new bits to FALSE. The behaviour of increasing the length of a bitwhich() object is different and depends on the content of the object:

  • TRUE – all included, new bits are set to TRUE

  • positive integers – some included, new bits are set to FALSE

  • negative integers – some excluded, new bits are set to TRUE

  • FALSE – all excluded:, new bits are set to FALSE

Decreasing the length of bit or bitwhich removes any previous information about the status bits above the new length.

Author

Jens Oehlschlägel

Examples


  stopifnot(length(ri(1, 1, 32)) == 32)

  x <- as.bit(ri(32, 32, 32))
  stopifnot(length(x) == 32)
  stopifnot(sum(x) == 1)
  length(x) <- 16
  stopifnot(length(x) == 16)
  stopifnot(sum(x) == 0)
  length(x) <- 32
  stopifnot(length(x) == 32)
  stopifnot(sum(x) == 0)

  x <- as.bit(ri(1, 1, 32))
  stopifnot(length(x) == 32)
  stopifnot(sum(x) == 1)
  length(x) <- 16
  stopifnot(length(x) == 16)
  stopifnot(sum(x) == 1)
  length(x) <- 32
  stopifnot(length(x) == 32)
  stopifnot(sum(x) == 1)

  x <- as.bitwhich(bit(32))
  stopifnot(length(x) == 32)
  stopifnot(sum(x) == 0)
  length(x) <- 16
  stopifnot(length(x) == 16)
  stopifnot(sum(x) == 0)
  length(x) <- 32
  stopifnot(length(x) == 32)
  stopifnot(sum(x) == 0)

  x <- as.bitwhich(!bit(32))
  stopifnot(length(x) == 32)
  stopifnot(sum(x) == 32)
  length(x) <- 16
  stopifnot(length(x) == 16)
  stopifnot(sum(x) == 16)
  length(x) <- 32
  stopifnot(length(x) == 32)
  stopifnot(sum(x) == 32)

  x <- as.bitwhich(ri(32, 32, 32))
  stopifnot(length(x) == 32)
  stopifnot(sum(x) == 1)
  length(x) <- 16
  stopifnot(length(x) == 16)
  stopifnot(sum(x) == 0)
  length(x) <- 32
  stopifnot(length(x) == 32)
  stopifnot(sum(x) == 0)

  x <- as.bitwhich(ri(2, 32, 32))
  stopifnot(length(x) == 32)
  stopifnot(sum(x) == 31)
  length(x) <- 16
  stopifnot(length(x) == 16)
  stopifnot(sum(x) == 15)
  length(x) <- 32
  stopifnot(length(x) == 32)
  stopifnot(sum(x) == 31)

  x <- as.bitwhich(ri(1, 1, 32))
  stopifnot(length(x) == 32)
  stopifnot(sum(x) == 1)
  length(x) <- 16
  stopifnot(length(x) == 16)
  stopifnot(sum(x) == 1)
  length(x) <- 32
  stopifnot(length(x) == 32)
  stopifnot(sum(x) == 1)

  x <- as.bitwhich(ri(1, 31, 32))
  stopifnot(length(x) == 32)
  stopifnot(sum(x) == 31)
  message("NOTE the change from 'some excluded' to 'all excluded' here")
#> NOTE the change from 'some excluded' to 'all excluded' here
  length(x) <- 16
  stopifnot(length(x) == 16)
  stopifnot(sum(x) == 16)
  length(x) <- 32
  stopifnot(length(x) == 32)
  stopifnot(sum(x) == 32)