An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090329/5ef31a73/attachment-0002.pl>
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:
I cannot find any R function or operator that performs a binary AND operation, as performed by Fortran built-in function "iand". Ideally either R operator "&" or "&&" should do that. But some tests proved they do not:
A<- 1
B <- 2
A
[1] 1
B
[1] 2
as.numeric(A&B)
[1] 1
as.numeric(A&&B)
[1] 1 The binary content of A should be "10000" The binary content of B should be "01000" Therefore the bitwise AND operator should yield 0 ! I do not know how to fore the binary representation of an unsigned integer when writing R code. Do you know how to get around this problem ? Thank you so much. Maura tutti i telefonini TIM! [[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of mauede at alice.it Sent: Sunday, March 29, 2009 12:23 PM To: r-help at r-project.org Subject: [R] binary AND operators in R I cannot find any R function or operator that performs a binary AND operation, as performed by Fortran built-in function "iand". Ideally either R operator "&" or "&&" should do that. But some tests proved they do not:
A<- 1 B <- 2 A
[1] 1
B
[1] 2
as.numeric(A&B)
[1] 1
as.numeric(A&&B)
[1] 1 The binary content of A should be "10000" The binary content of B should be "01000" Therefore the bitwise AND operator should yield 0 ! I do not know how to fore the binary representation of an unsigned integer when writing R code. Do you know how to get around this problem ? Thank you so much. Maura
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:
I cannot find any R function or operator that performs a binary AND operation, as performed by Fortran built-in function "iand". Ideally either R operator "&" or "&&" should do that. But some tests proved they do not:
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:
I cannot find any R function or operator that performs a binary AND operation, as performed by Fortran built-in function "iand". Ideally either R operator "&" or "&&" should do that. But some tests proved they do not:
R doesn't have an operator like iand built in, but I believe the packages bitops and bit provide something similar. Duncan Murdoch
A<- 1
B <- 2
A
[1] 1
B
[1] 2
as.numeric(A&B)
[1] 1
as.numeric(A&&B)
[1] 1 The binary content of A should be "10000" The binary content of B should be "01000" Therefore the bitwise AND operator should yield 0 ! I do not know how to fore the binary representation of an unsigned integer when writing R code. Do you know how to get around this problem ? Thank you so much. Maura tutti i telefonini TIM! [[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.