Page 1 of 1

Bitwise operators

Posted: Tue Jun 19, 2012 4:29 pm
by Juan Toranzo
I am new to this forum.

I would like to ask for an improvement to the bitwise operators documentation. Especially to the & (bitwise and), |(bitwise or), ^ (bitwise xor), and ~(bitwise not) operators.

For example:
(= (& 11 10) 10)
(= (& 111 100) 100)

as I expected, but
(= (& 1000 0010) 8) ; I was expecting 0
(= (& 11111 10101) 9061) ; I was expecting 10101
(= (& 0101 1000) 64)
(= (& 1010 1111) 82)
(= (& 1010 1111) 82)

Thanks in advance.

Re: Bitwise operators

Posted: Tue Jun 19, 2012 4:44 pm
by Lutz
1000 is not a binary number but decimal thousand. And 0010 is not a binary but interpreted as an octal number because it starts with a 0 (zero). So you are really doing a bit-wise & on the following bit patterns:

Code: Select all

> (bits 1000)
"1111101000"
> (bits 0010)
"1000"
Note, that other scripting languages, e.g. Python, Perl, Ruby treat those numbers the same way when using the bit-wise &. The following in Python:

Code: Select all

>>> 1000 & 0010
8
>>>
See also here: http://www.newlisp.org/downloads/newlis ... mbol_names