[Tutor] Defining "bit" type -- why not '!' ?

spir denis.spir at free.fr
Thu Jan 29 11:32:36 CET 2009

Le Thu, 29 Jan 2009 09:47:22 -0000,
"Alan Gauld" <alan.gauld at btinternet.com> a écrit :

> "Andre Engels" <andreengels at gmail.com> wrote
> >> Why not '!' for not, instead of '~'? I mean, '!' is used in logic,
> > in many languages and even in python (!=). On the other hand,
> > I had never encountered '~' meaning not.
> >
> > Although ! is indeed usual in computer languages, I disagree when 
> > you
> > say it is used in logic. There to my knowledge the standard is ¬, 
> > with
> > ~ being used if one wants to remain within easily-accessible 
> > character
> > sets.

Thank you for all your answers Albert, Andre, Alan (the 3A gang ;-}). Here is an overal and a trial to introduce my view on this topic.

I'm aware that "not 1" is not the same as "~1" in python. And I agree with that, while I disagree on how it is different. As I see it, both operators have a close meaning, so that using the same sign (and the same underlying method name, e.g. __not__) should be allright. This is a common practice in python and many languages that support OOP.
The flaw here in python is to treat integers as logical values, like if they were bools. This is for me a conceptual error. So that:

* If 'not' is only a plain logical operator, then "not 1" must raise TypeError.
* On the hand hand, "extended logic", meaning apply logic operators to non-logic values (e.g. "if not s" instead of "if s==''" or "isEmpty(s)"), is seen by many as a useful feature.
* If 'not' (or '!', or '~') has an extended meaning, and integers are additionally considered as byte sequences, then it is sensible to give "not 1" a semantics of bitwise not. So we don't need both "not 1" and "~1".
* In the latter case, if I understand correctly the new 'byte' type, then bitwise logical operations should rather be applied to byte, than to integers.
* The mix of "extended logic" on non-logical types and treating integers as bit sequences provakes a kind of conceptual collision.
* As a solution, bitwise operations may apply only to a type (byte or int) on which "extended logic" raises an TypeError.


la vida e estranya

More information about the Tutor mailing list