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 / Bitwise Operators

Author
Message
The Wilderbeast
19
Years of Service
User Offline
Joined: 14th Nov 2005
Location: UK
Posted: 5th Sep 2011 23:48
It's a bit annoying, but it seems that half of the operators from C have been implemented, and the other half haven't.

According to several posts from a search I did, there is supposed to be the Bitwise OR operator, but I can't get it working.

Any clues?


10% TGC Discount!
The Wilderbeast
19
Years of Service
User Offline
Joined: 14th Nov 2005
Location: UK
Posted: 6th Sep 2011 00:24
Never mind, on closer inspection of the help files the Bitwise OR operator is || (how confusing!).


10% TGC Discount!
JackDawson
13
Years of Service
User Offline
Joined: 12th Jul 2011
Location:
Posted: 6th Sep 2011 05:10
In case you missed it, I copied and pasted this for you from the DarkBasic Pro Help file.



"Life is like a box of chocolates.. eat it before it melts."
Green Gandalf
VIP Member
20
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 6th Sep 2011 19:12
What does the last one mean?



In fact the Help file is unhelpful. Try this:

JackDawson
13
Years of Service
User Offline
Joined: 12th Jul 2011
Location:
Posted: 6th Sep 2011 22:57 Edited at: 6th Sep 2011 22:58
hmm. the BITWISE NOT is set to reverse whats there. Example : 0111 will be changed to 1000. So from what i see in the help, it is correct.



In otherwords, unless my eyes are bad, that looks right to me.

"Life is like a box of chocolates.. eat it before it melts."
Green Gandalf
VIP Member
20
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 6th Sep 2011 23:30
Why? I get the value 4294967280 from my earlier snippet and the next one gives the same value -16 regardless:



The .. operator is a binary operator which needs two arguments and the Help file does not tell you how they are combined and I'm finding it impossible to predict what it does precisely. What do you think it does? If you change the first argument you get different values. What values would you expect and why?
JackDawson
13
Years of Service
User Offline
Joined: 12th Jul 2011
Location:
Posted: 7th Sep 2011 00:10 Edited at: 7th Sep 2011 00:14
All I see you doing is printing stuff to the screen. Your not using any BitWise commands on it. You have to tell it to use NOT.

Example : This AND this =.. The AND is part of the code.

You have to do that with NOT as well. You have to include that in your code.

This is the example that is from the help file :

IF NOT 0 THEN PRINT "this will print"
IF NOT 1 THEN PRINT "this will not print"

Its checking to see if its an unsigned number or not. If it can change, then it will flip it. If it cannot change, then it will not touch it. You cannot flip the Decimal number 0. There is no opposite. But a 1 can become a -1.

So look at the two examples again. If NOT tests the 0 and the answer still remains at 0 then it will print. If NOT tests a 1 and the answer is still not a 1 when it has been tested, then it will not print.

Get it now ?

"Life is like a box of chocolates.. eat it before it melts."
Green Gandalf
VIP Member
20
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 7th Sep 2011 00:21
What are you talking about?

Quote: "Get it now ?"


Well do you? The bit () you quoted from the help file clearly states that the symbol .. is a bitwise not operator (and the compiler thinks so too). The NOT operator is also bitwise but that is a unary operator not binary. Could you explain what expressions such as %1110..%1100 should evaluate to - using the Help file as guide of course? And provide a working snippet to demonstrate your point.
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 7th Sep 2011 00:21 Edited at: 7th Sep 2011 00:23
Quote: "What do you think it does? If you change the first argument you get different values. What values would you expect and why?"


I absolutely have no idea:


Now change that zero on line 2 to any other number ... did you see any difference in output? I don't. It seems like the '..' operator is a binary operator that ignores the second value and simply flips all of the bits.

If you want a bit-level NOT, then use the '~~' exclusive-or operator. That works as expected to flip the bits from the first parameter where they are set in the second.

Green Gandalf
VIP Member
20
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 7th Sep 2011 00:29
Quote: "It seems like the '..' operator is a binary operator that ignores the second value and simply flips all of the bits."


Er? All of the bits of what? The first value perhaps? The Help file says it flips the bits of the right value which is the second value as far as I can see. I can't reproduce the result claimed in the help file using any of the standard types - mainly because the leading blanks are treated as zeroes and are flipped as well.
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 7th Sep 2011 00:33
Quote: "The first value perhaps?"

Yes. I think I'm saying that the operation is bugged.

To state it more clearly: The result of the operation is the first value with all bits flipped, and is not affected by the second value at all.

Green Gandalf
VIP Member
20
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 7th Sep 2011 00:39
Quote: "I think I'm saying that the operation is bugged."


Or the Help file perhaps? Or perhaps the binary version of the NOT operator shouldn't be there at all?
JackDawson
13
Years of Service
User Offline
Joined: 12th Jul 2011
Location:
Posted: 7th Sep 2011 00:43 Edited at: 7th Sep 2011 00:51
Quote: "perhaps the binary version of the NOT operator shouldn't be there at all?"


You are exactly correct. It uses Whole Decimals. The NOT command is not for checking binary number per say. It checks the Decimals. What it does under the hood is check the decimal and converts it to binary. You cannot change a Binary number to another number with NOT. It doesn't work like that.

Here I found this to help you all out.

http://en.wikipedia.org/wiki/Bitwise_operation

Basically a NOT command is ONLY CHECKING the target number. It has nothing to do with printing it to the screen or anything else. It only checks the number to it's RIGHT. meaning NOT 0 or NOT 1. Having 0 NOT does nothing.

Using ".." ( without the quotes ) can also be used.

EXAMPLE :


In other words, you will ONLY see the one that is not flippable print to the screen. You cannot flip a 0 from a positive to a negative. Any other number you can.

"Life is like a box of chocolates.. eat it before it melts."
Green Gandalf
VIP Member
20
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 7th Sep 2011 01:18
You've completely missed the point - not helped perhaps by my use of the phrase "binary operator" to refer to the "non-unary operator" as distinct from a unary operator such as NOT (i.e. requires just a single argument). The .. operator requires TWO arguments and is a binary operator in that sense. It works on the binary version of the value as you point out - but no-one has suggested otherwise.

Quote: "You cannot change a Binary number to another number with NOT. It doesn't work like that."


Of course you can. What gives you that idea? Just try it and see - remembering that it works on all 32 bits of the number and will be printed as a signed integer. Better still try this:




Quote: "You cannot flip a 0 from a positive to a negative."


What do you mean?

Quote: "It has nothing to do with printing it to the screen or anything else."


What do you mean?

You can always print out the result of evaluating a numerical expression - and the result of a NOT operation, unary or non-unary, is no different in that respect.
JackDawson
13
Years of Service
User Offline
Joined: 12th Jul 2011
Location:
Posted: 7th Sep 2011 01:48
Quote: "not helped perhaps by my use of the phrase "binary operator" to refer to the "non-unary operator" as distinct from a unary operator such as NOT"


This is where our confusion probably was at then. We both are talking about two different things apparently. At least you are able to see what I was referring too. As for the % sign, I am not understanding DarkBasic Pro enough to know why that is associated with the BitWise NOT command. So I'll have to check into that part more.

Either way, thanks for the heads up.

"Life is like a box of chocolates.. eat it before it melts."
Green Gandalf
VIP Member
20
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 7th Sep 2011 02:16
Quote: "As for the % sign, I am not understanding DarkBasic Pro enough to know why that is associated with the BitWise NOT command"


It isn't. It's just a way of representing numbers in binary notation explicitly bit by bit. So, for example, the decimal 10 is the same as %1010. The binary representation is more convenient when working with the bitwise operators - because you can see easily which bits are affected. You could use decimal integers as well - but deciphering the results is just that much harder. For example, the expressions NOT 10 and NOT %1010 evaluate to the same result. Interestingly, the print command interprets the second as a dword and the first as an integer. The following snippet converts both to integers before printing.

JackDawson
13
Years of Service
User Offline
Joined: 12th Jul 2011
Location:
Posted: 7th Sep 2011 03:58
Hmm, I get different results when I use the % and when I take it away. Weird.

"Life is like a box of chocolates.. eat it before it melts."
Green Gandalf
VIP Member
20
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 7th Sep 2011 12:50
Quote: "Hmm, I get different results when I use the % and when I take it away."


What do you mean? Do you mean you get different results if you use "NOT 1010" instead of "NOT %1010"? If so then that's hardly surprising. The % symbol tells the compiler that the digits represent binary digits so %1010 is the decimal number 10 (=8+2). Without the % symbol the compiler thinks it's the decimal integer 1010 (one thousand and ten) - a quite different value entirely.
Pincho Paxton
22
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 7th Sep 2011 12:50
It works on binary, and decimal so you have a difference between 10, and 1010. %1010 is 10.

Green Gandalf
VIP Member
20
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 7th Sep 2011 12:51
Snap!

Login to post a reply

Server time is: 2025-06-28 05:33:59
Your offset time is: 2025-06-28 05:33:59