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 Studio Chat / Setting and clearing bits - follow-up question!

Author
Message
Jambo B
14
Years of Service
User Offline
Joined: 17th Sep 2009
Location: The Pit
Posted: 27th Oct 2020 11:07
Hello all,

This is doing my head in. I might be over-thinking it

If I had two 'flags' variables e.g.
Flag 1 = 4 || 8 || 32
Flag 2 = 16 || 32

And I wanted to copy a bit from Flag1 to Flag 2 - just duplicate the bit whether it was a 1 or a 0, how would I do that?

e.g. to copy the green zero from Flag 1 to Flag 2, 'overwriting' the red 1.

Flag 1 = 101010
Flag 2 = 101100

Thanks for any help
Preben
AGK Studio Developer
19
Years of Service
User Offline
Joined: 30th Jun 2004
Location:
Posted: 27th Oct 2020 13:10
Nice one , just remove the bit first so its always 0 , then add the bit if its set

flag1 = 2 //010
flag2 = 4+2 //110
bittocopy = 4

Print( bin(flag1) )
Print( bin(flag2) )

flag3 = (flag2 && !bittocopy) + (flag1 && bittocopy )

Print( bin(flag3) )

Subscribe and checkout great AppGameKit video's here: Videos click here
Latest GameGuru Loader news: News click here
Get GameGuru Loader PBR version here: Steam click here
best regards Preben Eriksen,
Jambo B
14
Years of Service
User Offline
Joined: 17th Sep 2009
Location: The Pit
Posted: 27th Oct 2020 13:40
Brilliant Preben, thanks!
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 27th Oct 2020 20:41 Edited at: 27th Oct 2020 20:42
As preben said bit manipulation is the go but you can also convert a binary string to a number using
number = val(binary_string, 2) // 2 is the base (Note ha 16 will convert a hex value)

So in your example you could say
flag3 = val(mid(bin(flag2),1,3)+mid(bin(flag1),4,1)+mid(bin(flag2),5,-1),2)

For me, easier to wrap my head around, but much slower
Raven
19
Years of Service
User Offline
Joined: 23rd Mar 2005
Location: Hertfordshire, England
Posted: 27th Oct 2020 21:31 Edited at: 4th Nov 2020 03:09


You can probably "Pretty" that up for your code., but it's a simple approach.
|| (Logical OR) is an Addition., while && (Logical AND) is a Subtraction

Now as a note a good approach for Case...Select., is to Define Flags
0x00 = Flag1
0x02 = Flag2
0x04 = Flag3
0x08 = Flag4
0x10 = Flag5
0x20 = Flag6
0x40 = Flag7
0x80 = Flag8

And this can go up to 32 Flags., so you can define all of them; (#Constant) then in specific code, have your Flag equal said Constant.
As such when you want to compare multiple Flags; you just add the necessary flags together (i.e. (Flag1 || Flag4 || Flag6 ) then simply have If Value = FlagCalculation.

The same is true in regards to checking them individually... because you've pre-defined them, doing something like
If (Value && Flag5) Then DoFlag5Stuff( )

Will work identical to the individual checks my functions do., my approach is merely if you don't pre-define them.
Remember that || (Logical OR) is "Addition" while && (Logical AND) is Equals; while
(Logical XOR) is "Subtraction" (see the code tag, as the forum translates ~ into - making them identical)
That's also good to keep in mind in setting and unsetting a Flag.
As Value = (Flag7 || Flag1 || Flag3) will set those flags... while
// will remove Flag3 from said Value

I would also point out the Data Formats

1234 is Decimal (1,234)
0x1234 is Hex (4,660)
%0101 is Binary (5) // this is read from Least Significant (Left) to Most Significant (Right)

Now on other platforms the way around for Binary is important, but AppGameKit will handle Big Endian and Little Endian for you, so it works "As Expected" on ARM and x86.
But know if you do switch to C++ this will ONLY work "As is" on x86

Hopefully this makes it all much clearer.
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 27th Oct 2020 21:51
Nice
Jambo B
14
Years of Service
User Offline
Joined: 17th Sep 2009
Location: The Pit
Posted: 27th Oct 2020 23:09
Thank you all very much for your input on this.

This is all great information and actually a useful tutorial into manipulation of bits using AppGameKit commands. It would be a useful addition to the AppGameKit docs.

Thanks again.
Scraggle
Moderator
20
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 29th Oct 2020 08:19 Edited at: 29th Oct 2020 08:21
Back in the day when we used to do 'Code Challenges' on the forum, I create an LCD display thingy that uses bit flagging and bit manipulation.
It doesn't need media, so you can run it as it is, see what it does and then check the code for how I played with the bits.


Attachments

Login to view attachments

Login to post a reply

Server time is: 2024-03-29 12:01:07
Your offset time is: 2024-03-29 12:01:07