Is "Not" bitwise, or boolean?

BlitzMax Forums/BlitzMax Programming/Is "Not" bitwise, or boolean?

sswift(Posted 2009) [#1]
I'm pretty sure the answer to that is bitwise, but as all the other bitwise operations are symbols, and the boolean operations are words, I would expect it to be boolean. And indeed, the help file says it is boolean.

Yet in this example:

Blah$ = "B"

If Blah$ = "A" Print "Equal."
If Not Blah$ = "A" Print "Not equal."


Setting Blah to "A" returns "Equal." However, setting it to "B" returns NOTHING.

The only way to get it to work properly is to encase the boolean operation in parentheses before performing the Not, like so:

Blah$ = "B"

If Blah$ = "A" Print "Equal."
If Not (Blah$ = "A") Print "Not equal."


Why is that?


klepto2(Posted 2009) [#2]
Not seems to have a higher priority than the '='.

With this in mind the code makes the following:
First it compares if Blah$ <> Null : Result = 0 (false)
After this it compares Result = "A" what also results in false. So the endresult is false.


ImaginaryHuman(Posted 2009) [#3]
You are seeing correct behavior switft.

First of all, `Not` is boolean. The bit-wise not is a single tilde ~ like a=~b. However, saying that, in effect, Not is also bit-wise, in the sense that it toggles the current boolean state from 1 to 0 and back again (true to false). I'm not sure what it would do to other bits if you tried to use it like bitwise.

Also I think that once you say `not`, you are asking for it to be followed by some expression which will be evaluated as a boolean. I think it's going to do `If Not Blah$`. Which is the same thing as saying, `If Blah$ as an ascii or unicode value has bit 1 set to a 0, don't print anything`. (edit - I think klepto has it explained better with the =false thing)

It's interpreting the value of Blah$ as a boolean instead of doing the = after it.

If you think about it, you're trying to do two `equals` one after another. You're saying:

If ((Blah$="B")=False)

and I think because of that it's going to consider the boolean expression ended when it reaches the first = sign. Otherwise it'd be like if Blah$="B"=False which wouldn't make sense.

Usually when I use boolean stuff like And Or Not I encapsulate each section of the expression in brackets to make sure it's going to do what I am thinking it should do.


sswift(Posted 2009) [#4]
So what you're saying is it's interpreting:
If Not A = B

Like so:
If (Not A) = B

Instead of what I wasnted, which was:
If Not (A=B)


On a related note, sometimes I wish I could type the following:

If A = 10 Or 20

And have that work. But it won't. :-(

And yes, I am aware of the Select statement. Just sometimes it'd be less typing if I could do that with an If statement.


Jesse(Posted 2009) [#5]
this is interesting:
b$ = Null
b$ = Not b$
Print b$
Print Asc(b$)



sswift(Posted 2009) [#6]
Null = 0
B$ = Null
Not Null = 1
B$ = Not Null
B$ = 1
The ascii code for 1 is 47.

Nothing strange there.


Jesse(Posted 2009) [#7]
if it's a string. why should it have a value of "1"?
if it was an integer, you would be correct but it has nothing to do with the string value of null. if anything it should be chr(%01).

Let see, string variables are pointers as far as I know or understand. If a string is null it points to nothing and is considered to have a 0 value, not "0" literally but point to memory address 0.
If a string has at least one character, it points to the address of that specified character. If it has more than one characters, it stil points to the address of the first character so how does the compiler figure to put the number "1" or chr(47) as the value. I am guessing it is just one of those quick fixes by Mark.

[edited]


ziggy(Posted 2009) [#8]
It's boolean.
Local i:Int = 0
Print Not(i)

It prints 1 wich it means it change only the less significant bit, ignoring the rest of them.

Bitwise not is this symbol ~
Local i:Int = 0
Print ~i

if prints -1 wich is the bitwise negation of 0 on a singed int.

EDIT: Not really, ~ is the exclusive OR... !!!


Floyd(Posted 2009) [#9]
It's obvious if you look at all the bits.
n = 1234512345

Print
Print Bin( n )
Print Bin( ~n )



Azathoth(Posted 2009) [#10]
Not really, ~ is the exclusive OR... !!!
No, the binary usage is XOR.

Edit: Blitzmax uses ~ as both an unary and binary operator.


TomToad(Posted 2009) [#11]
if it's a string. why should it have a value of "1"?
if it was an integer, you would be correct but it has nothing to do with the string value of null. if anything it should be chr(%01).

Not works on integers. So B$ is converted to an Int first, which is 0. Then Not 0 becomes 1, then the 1 is converted back to a string and assigned to B$.
Under the hood it is basically doing

b$ = Null

temp = Int(b$)
temp = Not temp
b$ = temp

Print b$
Print Asc(b$)



Jesse(Posted 2009) [#12]
obviously it was doing something weard like that. I just don't buy it as an logical answer for it. is that how other languages deal with it? or even other basics?


SculptureOfSoul(Posted 2009) [#13]
On a related note, sometimes I wish I could type the following:

If A = 10 Or 20

And have that work. But it won't. :-(

And yes, I am aware of the Select statement. Just sometimes it'd be less typing if I could do that with an If statement.


You should look into Lisp. You can easily create new syntax that looks and behaves just like the original language syntax. You'd just build a Lisp macro (much different and more powerful than standard macros) and could have functionality such as this, easily
(Custom-If 
   ((A = 10) 
       OR 
         (A = 12 AND B = 15) 
            OR 
              ((A=55 OR B=20) AND C= 5))  
    (DoStuff))


In this case, the function DoStuff would be called if A=10, or if A=12 and B = 15, or if A = 55 or B= 20 and C = 5 (rather self explanatory, I suppose).

Of course, you'd have to write the Custom-If macro, but it's a one time affair that can then be used time and time again. The macro is expanded prior to compilation and converts all of the arguments to the macro to valid lisp function calls, and so there is no overhead, either, as the code the compiler sees would be the far uglier code if you had written it by hand without the aid of your custom macro.