Skip to content

binary AND operators in R

5 messages · mauede at alice.it, Domenico Vistocco, Daniel Nordlund +2 more

#
You should find the functions:

 bitAnd, bitOr and bitXor

in the bitops package.

Ciao,
domenico
mauede at alice.it wrote:
#
Maura,

 I tried the search command 

??bitwise

And found the package bitops on CRAN.  Check and see if it has what you
need.

Hope this is helpful,

Dan

Daniel Nordlund
Bothell, WA USA
#
mauede at alice.it wrote:
they do not, it seems clear from the documentation.  here's a hint:

    RSiteSearch('bitwise')



your question points me to another issue (thanks!):

    one = as.raw(1)
    as.logical(one)
    # TRUE

    two = as.raw(2)
    as.logical(two)
    # TRUE

    one & two
    # 00

    if (one) 1 else 0
    # error: unimplemented type 'raw' in 'asLogical'

oops...

how come 01 AND 02 = 00??  well, you see, & is a *logical* (not a
*bitwise*) AND operator (see ?'&'), but for raws it does bitwise AND
(see ?'&' again):

    as.raw(6) & as.raw(3)
    # 02

?'&' says:

"
Arguments:

    x, y: logical vectors, or objects which can be coerced to such or
          for which methods have been written.
"  

this might have been more explicit about raws, but the use of logical
operators (that is, bitwise operators) with raws *is* described in the
details.

the other case is weird; ?'if' says:

"
Arguments:

    cond: A length-one logical vector that is not 'NA'. Conditions of
          length greater than one are accepted with a warning, but only
          the first element is used.  Other types are coerced to
          logical if possible, ignoring any class.
"

and the help page does not mention 'raw' at all.  raws are not logical:

    is.logical(one)
    # FALSE

(what about the type hierarchy???), yet it is possible to convert them
to logical:

    as.logical(one)
    # TRUE

why if(one) chooses to raise an error is a mystery to me.  the null
hypothesis:  a design flaw.  the alternative one:  a bug.  choose your test.

best,
vQ
#
mauede at alice.it wrote:
R doesn't have an operator like iand built in, but I believe the 
packages bitops and bit provide something similar.

Duncan Murdoch