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 / quick question about bitwise operators/dword stuff (is 0 really 0?)

Author
Message
Aralox
19
Years of Service
User Offline
Joined: 16th Jan 2007
Location: Melbourne
Posted: 28th Jul 2010 04:02 Edited at: 28th Jul 2010 05:00
Hey there, why doesnt "thingo" print?

is thing && %0010 not equal to the integer 0, but some kind of dword zero?

I'm new to bitwise operations, so any tutorial links would be appreciated (ive read a few c++ ones just to learn what the operators eg ~~ and && do, but i know theres a lot more to this)

Thanks,
Aralox

EDIT: I found a quick workaround to this - just let x = thing && %0010 then check if x is zero. Id still like to know whats going on though. (im guessing that letting it equal to x forces it to be an integer?)

Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 28th Jul 2010 07:16 Edited at: 28th Jul 2010 07:18


In the expression you're masking (ANDING) the constants of THING with 2 (%0010), then comparing the result with 0

So the expression will only be TRUE (1) when bit 2 isn't set in the THING variable. Your code sets this bit, so it doesn't print anything.

So we can detect on/off state with something like the following.


Now that's assuming Dbpro treats bit operators at higher precedence than compares. You may well have bracket the mask to ensure this is done first.

ie,



Aralox
19
Years of Service
User Offline
Joined: 16th Jan 2007
Location: Melbourne
Posted: 28th Jul 2010 08:42 Edited at: 28th Jul 2010 09:13
Thanks for the reply Kevin , i appreciate it.
If you mean 'setting the bit' as making that bit = 1, then Im quite sure my code does not do that, i.e. I declared thing as a dword, so its default zero.
But you found the problem - the expression returns true when thing && %0010 is in brackets.
What do you mean by compares?


EDIT: ahh wait a minute i think i get it: so what dbp is doing is checking if the expression '&0010 = 0' is true (which its not) before checking 'thing && %0010', so the overall expression ends up becoming 'thing && 0'. but this should return zero too....

Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 29th Jul 2010 05:47
If THING is zero (sorry, misread your original example),and the expression returns false (zero), then it proves that the comparison (the ='s) is performed before the AND, as you're already worked out.


In BASIC generally, the ='s symbol has two operations, it's an assignment operator and a equality test.


So,
A=B ; = is used as an assignment.

SomeArray(100)=34543 ; = is used as an assignment.


Now here it's being used as both,

A=B=C

So the language is comparing if B ='s C, then the returned result is stored in A

Aralox
19
Years of Service
User Offline
Joined: 16th Jan 2007
Location: Melbourne
Posted: 29th Jul 2010 11:28
Ahh i see so for thing && %0010 = 0, the expression will become thing && 0, which will return a false. Since my original IF was if thing && %0010 = 0, it will return false, so the print does not happen. It makes sense how 'if (thing && %0010 = 0) = 0' returns true.

Thanks again,
Aralox

Login to post a reply

Server time is: 2026-07-26 09:56:06
Your offset time is: 2026-07-26 09:56:06