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.

AppGameKit Classic Chat / Bitwise operators question

Author
Message
Jambo B
15
Years of Service
User Offline
Joined: 17th Sep 2009
Location: The Pit
Posted: 4th Nov 2017 14:50
Hi all,

Wonder if anyone can help me with this. I'm trying to control the right-most bit of a binary number.

Switch the first bit on when it's 0. This seems to work:
Value = %110
Value = Value && %1
Result = %111

Switch the first bit on when it's already 1. This seems to work:
Value = %111
Value = Value && %1
Result = %111

Question is, how do I switch the first bit OFF when it's already 0, or when it's 1?

Thanks for any advice,

James
Kevin Picone
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 4th Nov 2017 14:54
use AND to clear the bit/bits you want, OR to set bit.



PlayBASIC To HTML5/WEB - Convert PlayBASIC To Machine Code
Jambo B
15
Years of Service
User Offline
Joined: 17th Sep 2009
Location: The Pit
Posted: 5th Nov 2017 10:54 Edited at: 5th Nov 2017 11:00
Thanks, Kevin.

Let's say that each of the bits represented an option. Permissions = %11011

Enabled | Read | Write | Erase | Format
......1.............1............0...........1............1......

I could add the write option like this: Permissions = Permissions || %100

If I wanted to set the Read permission to 0, without interfering with the other options, how would I do that?

If I use Permissions = Permissions && %0000, then the write, erase and format options would be affected. If I use Permissions = Permissions && %0111, they would also be affected.

Am I missing some trick here with bitwise operators (probably!)

Thanks for any advice,

James
Jambo B
15
Years of Service
User Offline
Joined: 17th Sep 2009
Location: The Pit
Posted: 5th Nov 2017 11:08
The only way I can think of doing it is this:



But this doesn't look very elegant to me... am I misunderstanding something?
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 5th Nov 2017 13:13 Edited at: 5th Nov 2017 13:21
The way I would do this is to set each 'flag' to the value of the next binary digit (1,2,4,8,16 etc.)
You can then store an integer that contains all the flags and use a binary comparison to see if they are set.
Simpy add or subtract the integer value of the flag from the permission integer to change the settings.

Like this:
Jambo B
15
Years of Service
User Offline
Joined: 17th Sep 2009
Location: The Pit
Posted: 5th Nov 2017 15:27
Thanks, Scraggle - I'll do it like that.

James
PSY
Developer
8
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 5th Nov 2017 17:25 Edited at: 5th Nov 2017 17:27
Pretty complicated for such a simple task...


You just use XOR and set the bit you want to TOGGLE to 1
If the bit was 0, it will be set to 1 and vice versa

Using your example:
Value = %11011
Value = Value ~~ %1000 ( toggle READ )
Result = %10011


PSY
PSY LABS Games
Coders don't die, they just gosub without return
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 5th Nov 2017 19:39
The problem is, he doesn't want to toggle the value. He wants to set it to zero. I'm not sure you can do it with XOR.
You need an IF to check the bit before setting.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
PSY
Developer
8
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 5th Nov 2017 21:51 Edited at: 5th Nov 2017 21:52
Then he needs to do it just as Kevin Picone said.

Any bit, 0 or 1, will be 0 after an AND operation with 0
Any bit, 0 or 1, will be 1 after an OR operation with 1


Cheers,
PSY
PSY LABS Games
Coders don't die, they just gosub without return
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 5th Nov 2017 21:56 Edited at: 5th Nov 2017 21:57
Quote: "Switch the first bit on when it's 0. This seems to work:
Value = %110
Value = Value && %1
Result = %111"


I think this is incorrect. && (And) will only set the bit on when both bits are on. The result of that operation would be %110

You could force the bit off using something like this
Value = %111
Value = Value && %110
Result = %110
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 6th Nov 2017 02:00
I did a bitwise tutorial awhile back, may help out.
http://www.zimnox.com/resources/articles/tutorials/?ar=t002


"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Jambo B
15
Years of Service
User Offline
Joined: 17th Sep 2009
Location: The Pit
Posted: 6th Nov 2017 19:06
Thanks to all for your help. After reading all the comments, I'm pretty convinced that only an 'if' statement will allow us to set a single bit to zero (without knowing what it was before):

if (Permissions && %1000)
dec Permissions, %1000
endif

Any other ands, ors, xors or subtractions either affect the other bits, or mess up the binary number completely, e.g. subtracting %10 from %1001 will cause the other bits to change.

Thanks again,

James
Jambo B
15
Years of Service
User Offline
Joined: 17th Sep 2009
Location: The Pit
Posted: 6th Nov 2017 19:08
@blink0k - you're right. That should've been an OR not an AND.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 6th Nov 2017 19:47 Edited at: 6th Nov 2017 19:48
Quote: " I'm pretty convinced that only an 'if' statement will allow us to set a single bit to zero ("


X = whatever integer you wish to set the right-most bit to 0 (or the first bit)
4294967294 = 4 bytes, all bits set to 1 except the right-most bit which is left to 0

X = X & 4294967294

Just as Kevin suggested, using AND to single out the last bit. More specifically, we're just omitting it.


You can write a little function and, with a little bit-shifting, easily single out and retrieve or set any bit you want. I'm pretty sure I've written such a function before.

Oops, I see that's what Blink already said as well.

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Jambo B
15
Years of Service
User Offline
Joined: 17th Sep 2009
Location: The Pit
Posted: 6th Nov 2017 21:03 Edited at: 6th Nov 2017 21:07
Thanks, Phaelax.

Please excuse me if I'm being a bit thick (this has happened before ) But let's hypothetically say that I have a bit pattern like this: %??0??

The question marks represent bits whose value is unknown - they could be 1s or 0s. They relate to other options within the program.

The user specifies an option which, when stored, sets the middle bit to zero.
But the problem is, it's already zero.
We can't AND the pattern with %000 or %011 to get the middle bit to zero, because that will also affect the right-most two digits - and we don't know what they are.
So how could we set the middle bit to zero without testing first whether it was already zero? The only way I can think of is (as you say) writing a function which tests the bit, and then sets it to 0 if it's 1. Probably by subtracting %100. And if it's not 1, it leaves it alone without subtracting at all.

Sorry if I'm rambling - it's been a long day!

James
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 6th Nov 2017 22:24 Edited at: 6th Nov 2017 22:25
You'd do it exactly the way I did it in my example. The only way mine is different is that I used a decimal representation of the parameter flag.
When you want to check %??0?? That is the same as checking the decimal number 4 or (in my example) checking the flag cWRITE

You can change everything to binary first if it makes you happy but may way is much more human friendly.
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 6th Nov 2017 23:20 Edited at: 6th Nov 2017 23:21
Your mask should have all the bits that you want to leave unchanged set to 1 and all the bits you wants to set off as 0

So in your example
Value = %??0??

Value && 11011 // (AND) Will set the third bit to zero no matter what it is set to and preserve the values of the other bits
PSY
Developer
8
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 7th Nov 2017 02:25 Edited at: 7th Nov 2017 02:26
Quote: "Value && 11011 // (AND) Will set the third bit to zero no matter what it is set to and preserve the values of the other bits"


EXACTLY

And to activate the 3rd bit:
Value || 00100 // (OR) Will set the third bit to one no matter what it is set to and preserve the values of the other bits
PSY LABS Games
Coders don't die, they just gosub without return
Jambo B
15
Years of Service
User Offline
Joined: 17th Sep 2009
Location: The Pit
Posted: 7th Nov 2017 17:08
Ah! The penny has (finally!) dropped. Thanks to you all for your help (and patience!)

We live and learn, eh?

James
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 7th Nov 2017 18:05 Edited at: 7th Nov 2017 18:06
Sounds like the others explained it before I could get back. I probably should've used the term "mask" somewhere.





In theory I think this should work. I'm not 100% certain AppGameKit has the bitwise complement operand.

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Jambo B
15
Years of Service
User Offline
Joined: 17th Sep 2009
Location: The Pit
Posted: 12th Nov 2017 14:08 Edited at: 12th Nov 2017 14:16
Thanks to all of you who helped with this. I think I've cracked it. Posting this in case anyone else needs to do something similar.

At Phaelax and Scraggle's suggestions, I wrote a few simple functions to set/clear bits. Here's a program showing the functions working, and the functions themselves.



Calling stbit() and clbit() will set/clear the appropriate bits of the number without affecting the other bits. Setting an already-set bit does not cause a problem, nor does clearing an already-clear bit.

e.g. Flags = clbit(Flags,0) will clear the right-most bit of Flags.

There's perhaps an easier/faster way to do it than this, but it seems to be working for me at the moment. I also believe that the bits should be labelled 1-31, not 0-30 - but 0-30 worked with the keyboard!

Thanks again to all of you for your help.

Jambo

Login to post a reply

Server time is: 2024-11-24 06:13:46
Your offset time is: 2024-11-24 06:13:46