[Tutor] & syntax

Alan Gauld alan.gauld at btinternet.com
Tue May 27 18:55:36 CEST 2008


"kinuthia muchane" <muchanek at gmail.com> wrote

>>>> 2&1
> 0
>>>> 3&1
> 1
>
> Would someone kindly explain to me what is going on here. All I can 
> see
> is even numbers return a '0' and odds a '1'. I also know all even
> numbers in binary  end in a zero. I had thought '&' was another way 
> of
> writing the 'and' operator, seems I was wrong.

This is a bitwise AND, ie it operates on each of the binary bits
independanly. OTOH The logical AND operator treats the value
*as a whole* as a boolean (true/false) value and operates
on that value.

You can read about bitwise operators and how and why they are used
in my tutorial in the Using the OS topic, about half way through.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list