Bitwise operators

Started by Juan Toranzo, June 19, 2012, 09:29:52 AM

Previous topic - Next topic

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.

Lutz

#1
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:



> (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:


>>> 1000 & 0010
8
>>>


See also here: http://www.newlisp.org/downloads/newlisp_manual.html#symbol_names">http://www.newlisp.org/downloads/newlis ... mbol_names">http://www.newlisp.org/downloads/newlisp_manual.html#symbol_names