Bitwise operators

Q&A's, tips, howto's
Locked
Juan Toranzo
Posts: 1
Joined: Tue Jun 19, 2012 4:04 pm

Bitwise operators

Post 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.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: Bitwise operators

Post 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

Locked