Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / DBP's AND operator

Author
Message
ShellfishGames
13
Years of Service
User Offline
Joined: 6th Feb 2013
Location:
Posted: 17th Mar 2014 14:49
I'm shocked.

Is it true that DBPs and is basically just a bitwise and operator? It's funny because I've been using DBP for about 10 years now on a daily basis and never realized this.



That's really strange, given that "if <any value other than 0>" is evaluated to true so both "if 2" and "if 1" will be true, but "if 2 and 1" apparently is not, because 2 && 1 = 0.

This kind of blew my mind... did the and operator change in one of the more recent updates, or has it always been this way?
...I still can't believe this never caused any trouble in any of my programs until now.

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 17th Mar 2014 16:18
Quote: "This kind of blew my mind... did the and operator change in one of the more recent updates"


It was changed in an update - but a long time ago. For some odd reason it caused a bit of a stir.

Why would you ever want to do "2 and 1" if not bitwise?

In my opinion, logical operators should be reserved for use with true and false, 0 and 1, etc, not mixed up with other number systems. It doesn't make much sense to me to use "if 2". What on earth is that supposed to mean "if something = 2" or "if 2>0" or what? Hardly makes for easy reading of code.



Powered by Free Banners
ShellfishGames
13
Years of Service
User Offline
Joined: 6th Feb 2013
Location:
Posted: 17th Mar 2014 17:27 Edited at: 17th Mar 2014 17:28
For instance, I've got a set of GUI functions, among them a button function returning 1 if the button is clicked and 2 if it his hovered (and 0 otherwise). Imagine now I wanted to display a string somewhere explaining what the button does, but only when the mouse is on that button - usually I just do something like this:



This, however, isn't possible anymore when combining it with other conditions using the AND operator, in this case I'd have to write "if r<>0 and ..." instead of "if r and ...". Which certainly isn't a problem. It just bugs me a little as it sort of "ruins" my personal standards of writing code. (And because it's not consistent.. I like consistency)

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 17th Mar 2014 19:39
Quote: "This, however, isn't possible anymore when combining it with other conditions using the AND operator, in this case I'd have to write "if r<>0 and ..." instead of "if r and ...". "


I don't see the problem. I would ensure that any such function returned true/false or 1/0 if it was intended to be used that way. Then you can have things like "if r" , "if r and s" , and so on.

Quote: "And because it's not consistent"


I thought it was consistent. Where's the inconsistency? [This probably isn't an issue in languages which insist on Boolean types in such situations.]



Powered by Free Banners
Mobiius
Valued Member
23
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 17th Mar 2014 20:35
Why not just use a case statement?



Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 17th Mar 2014 22:04
Indeed. You could use the following instead - and just add new cases when you need them.






Powered by Free Banners
Rudolpho
20
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 17th Mar 2014 23:03
Quote: "It doesn't make much sense to me to use "if 2". What on earth is that supposed to mean "if something = 2" or "if 2>0" or what? Hardly makes for easy reading of code."

It usually means "if 2 != 0" (or "2 <> 0" in dbp-esque syntax). To me that makes sense since false <=> 0 in most (common) languages.


"Why do programmers get Halloween and Christmas mixed up?"
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 17th Mar 2014 23:18
Quote: "To me that makes sense since false <=> 0 in most (common) languages."


And the connection with "2" is what exactly?



Powered by Free Banners
Rudolpho
20
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 17th Mar 2014 23:38
It doesn't make much sense with a constant numeric value like that, but you can use it to check if a variable has been "set" (not zero) etc. It is common practice to check if a pointer is set in C/++ through "if(ptr) ...".


"Why do programmers get Halloween and Christmas mixed up?"
ShellfishGames
13
Years of Service
User Offline
Joined: 6th Feb 2013
Location:
Posted: 17th Mar 2014 23:39
Well of course I could use a case block - or just the "if ..<>0" version. It really isn't much of a problem. I'm just (still) really astonished that I never realized that AND and && are the same in DBP.

Quote: " I would ensure that any such function returned true/false or 1/0 if it was intended to be used that way."


Well, as I said, the function returns either 0 or 1 or 2, depending on the conditions (i.e. what the mouse does), so making sure the function returns a boolean wouldn't really help, it would just make things more complicated.

Quote: "I thought it was consistent. Where's the inconsistency?"


If x is true and y is true, you'd assume "x and y" is true as well. In DBP both 1 and 2 are true when used alone, but "1 and 2" is evaluated to false. That seems kind of inconsitent to me.

Again.. this isn't really a problem. I just find it very remarkable.
Why does the && operator exist in the first place, if it's exactly the same as and?

Rudolpho
20
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 17th Mar 2014 23:52
I agree that it is weird and it probably explains some logic faults I had a few years back. I just started writing more explicit checks like "if a = true and b = true", but you shouldn't have to do that. The fact that there is another bitwise AND operator, as well as the fact that other logical operations use textual operators and the bitwise ones use symbols, make this an almost guaranteed cause for headache. I took a look through the source code from the google code repository but couldn't find any implementations for the logical operators there (some bitwise ops are implemented as functions in DBProCore.dll though) so I guess not much can be done about it without a recompilation of the compiler.


"Why do programmers get Halloween and Christmas mixed up?"
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 18th Mar 2014 00:00 Edited at: 18th Mar 2014 00:06
Quote: "If x is true and y is true, you'd assume "x and y" is true as well."


I wouldn't use either unless x and y were Boolean, in which case there's no problem. I really don't see the need to try to use general positive integers as true or false - unless you really intend to test the individual bits which I guess is the idea behind the bitwise interpretation for use with flags etc.

Edit By the way, this issue has been around a long time:

bitwise AND

IanM's claim that it had always been this way surprised me because I thought I remembered the change.



Powered by Free Banners
ShellfishGames
13
Years of Service
User Offline
Joined: 6th Feb 2013
Location:
Posted: 18th Mar 2014 00:21 Edited at: 18th Mar 2014 00:22
Quote: "I really don't see the need to try to use general positive integers as true or false"


Well, yes... that's really just a matter of habit I guess. And given that the possibility of using integers instead of booleans exists in DBPro, I assumed it would fully support this by making sure the logical operators work similarly.

I mean, you probably don't write "if someBooleanVariable = 1", but simply "if someBooelanVariable". Similarly, I like to write "if someIntegerVariable" instead of "if someIntegerVariable <> 0", just because it looks better and makes things easier to read (for me, that is).

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 18th Mar 2014 00:48
Quote: " just because it looks better and makes things easier to read (for me, that is)."


That sums up my view too - of the other approach. I guess what works for one person doesn't work for someone else.

I suspect I was thinking of the NOT operator in connection with upgrades - but I haven't the energy to track it down. Variants of this bitwise discussion crop up from time to time and I wouldn't be surprised if there were a few rejected bug reports relating to this.

This topic needs to be aired from time to time because it catches people out now and then.

Here's a fun variant. Run the following



Then remove the brackets and try again.

I guess we can both agree that 1 is not -2 ...



Powered by Free Banners
ShellfishGames
13
Years of Service
User Offline
Joined: 6th Feb 2013
Location:
Posted: 18th Mar 2014 00:54 Edited at: 18th Mar 2014 01:11
Woah.. so not simply inverts all the bits? That pretty much explains why I never use not. I always thought it had strange bugs, but apparently it just behaves... well... different from what I would expect. But why would "not 1" return something different than 0? Does that make any sense?

Edit: ...what.



Since when can boolean variables have values other than 0 or 1? What's going on here? Go home DBP, you're drunk.

..or to go a step further:



"if x and y" evaluates to FALSE here despite x and y both being true boolean variables.

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 18th Mar 2014 01:11
Quote: "But why would "not 1" return something different than 0? Does that make any sense?"


In the world of bitwise arithmetic it does.

DBPro uses 32 bit variables mostly, so 1 is really %00000000000000000000000000000001, i.e. 31 zeroes followed by a 1. Hence not 1 is 31 1s followed by a zero, i.e. %11111111111111111111111111111110. But that is a dword and DBPro converts to an integer by default where the top half of the range is for negative integers. You can see the dword equivalent as follows:



Coding is fun.



Powered by Free Banners
ShellfishGames
13
Years of Service
User Offline
Joined: 6th Feb 2013
Location:
Posted: 18th Mar 2014 01:19
Quote: "In the world of bitwise arithmetic it does."


But in the world of a programmer who's using the not operator, it doesn't.
Sure, a "bitwise not" is meant to work this way. But usually you'd write things such as "if not object exist(someObject)" - which, apparently, is always true in DBP, given that not 0 and not 1 both return values other than 0, hence both evaluate to TRUE and the THEN-branch gets executed either way.. in other words, in what practical case would anybody even use this not operator, apart from the rare occurences where you need actual bitwise operators?

Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 18th Mar 2014 01:49
Not seems to predictable enough behind an IF statement, but it may well be looking into the content in which it's used.



Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 18th Mar 2014 01:49
Quote: "which, apparently, is always true in DBP"


Are you sure?



I've a vague feeling that the NOT operator is interpreted differently in an if clause.

Another good reason for not using not.



Powered by Free Banners
ShellfishGames
13
Years of Service
User Offline
Joined: 6th Feb 2013
Location:
Posted: 18th Mar 2014 01:59
This is certainly interesting.




Quote: "Another good reason for not using not."


Absolutely...

Rudolpho
20
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 18th Mar 2014 11:35
Ugly but hey, it works...



"Why do programmers get Halloween and Christmas mixed up?"
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 18th Mar 2014 11:57
Am I not totally confused now?



Powered by Free Banners
Mobiius
Valued Member
23
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 19th Mar 2014 11:22
Quote: "Since when can boolean variables have values other than 0 or 1?"

Since DBPro doesn't have a boolean datatype, and uses integer instead.

ShellfishGames
13
Years of Service
User Offline
Joined: 6th Feb 2013
Location:
Posted: 19th Mar 2014 11:53
Quote: "Since DBPro doesn't have a boolean datatype, and uses integer instead."


Actually it seems to use bytes:



Well, I guess it all makes sense now. In some cases throughout the past I had problems that, as I thought, were caused by integer values given to functions as boolean parameters. But apparently the problems were never caused by an integer value of 2 converted to a boolean value of 0, but rather the logical binary operators being bitwise and the boolean variables keeping the old not-so-boolean values.

timer help
12
Years of Service
User Offline
Joined: 14th Jan 2014
Location:
Posted: 19th Mar 2014 18:12
maybe it didn't belive in its self
tiffer
20
Years of Service
User Offline
Joined: 6th Apr 2006
Location: Scotland
Posted: 20th Mar 2014 15:37
Quote: "Actually it seems to use bytes:"

So I'm not really saving smem memory by using boolean in arrays?

Cwatson
tiffer
20
Years of Service
User Offline
Joined: 6th Apr 2006
Location: Scotland
Posted: 20th Mar 2014 15:37
'timer' shouldn't you be concentrating on your code instead of writing nonsense everywhere?

Cwatson
timer help
12
Years of Service
User Offline
Joined: 14th Jan 2014
Location:
Posted: 20th Mar 2014 17:56
Quote: "'timer' shouldn't you be concentrating on your code instead of writing nonsense everywhere?"


Well see, im trying to get some motivation for the person that needs help. If you believe, that's motivation. This is a way how to help people if I suck at coding.
pcRaider
19
Years of Service
User Offline
Joined: 30th May 2007
Location:
Posted: 20th Mar 2014 18:22
AND operator


windowsXp
tiffer
20
Years of Service
User Offline
Joined: 6th Apr 2006
Location: Scotland
Posted: 20th Mar 2014 18:45 Edited at: 20th Mar 2014 18:46
No one's saying you suck. I just thought your time would be better served trying to solve the issues with your own project.

Cwatson
Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 20th Mar 2014 18:59
Quote: "So I'm not really saving smem memory by using boolean in arrays?"


Nope!!

The reason I do not use booleans in DBP.

I always thought the difference between DBP's && operator and the 'AND' falls down to precidence. They both appearto be binary operators.



Login to post a reply

Server time is: 2026-07-06 05:02:58
Your offset time is: 2026-07-06 05:02:58