Jump to content

Photo

getting bits of a integer and storing them in bools


  • Please log in to reply
3 replies to this topic

#1 Shadowblitz16

Shadowblitz16

    Illustrious

  • Members

Posted 03 January 2017 - 09:25 PM

does any body know how to do something like this

Spoiler

where the state is a int and the bits are flags for my npc?


Edited by Shadowblitz16, 03 January 2017 - 09:28 PM.


#2 fireyf

fireyf

    Initiate

  • Members

Posted 03 January 2017 - 11:18 PM

I've never scripted for ZC before.  How similar is the code to C?  If it is similar enough you can do bit masking.  I'll see if I can explain it to you adequately.  The operators you'll need are & (AND) | (OR) ^ (Exclusive OR XOR for short).  If you use the XOR you can compare the bits between two values to see where they are the same.  For example if you had 0000111100001111 (binary) and did an XOR with 0000111100000000 you would get 0000000000001111 as your result.

 

0000111100001111 ^ 0000111100000000 = 0000000000001111  which would tell you you have a value of 15 in your last byte because that is the only part of your int that is an exclusive or to the other.  So basically you'd want to XOR your int with a value that you know would give you a result with the 1's in the spot you're looking for them in.

 

y = 15;

 

x ^ y = 15 would mean that you have all ones in the last byte of X.

 

A regular or (the | ) would tell you where you had a 1 in the bit of one int but a 0 or a 1 in the other.  & would tell you where you have a 1 in both.

Clear as mud?

I hope that helps and again, I'm assuming the scripting language is similar enough to C and C++ so that this will work for you.

 

edit

Now that I think about it, the best comparison to use would be to XOR compare to an int with 1's in every spot where you don't want to find 1's and 0's where you want to find 1's


Edited by fireyf, 03 January 2017 - 11:24 PM.


#3 Timelord

Timelord

    The Timelord

  • Banned
  • Location:Prydon Academy

Posted 04 January 2017 - 12:39 AM

value |= flag; //Set a flag on a variable.
value &= ~flag; //Unset a flag on a variable.

if ( ( value&flag) != 0 ) //check a flag on a variable. Be sure to nest the binary operation, so that it has operational priority.

0  = 0000 = weak small surfaced bouncer
1  = 0001 = weak small surfaced dweller
2  = 0010 = weak small buried bouncer
3  = 0011 = weak small buried dweller
4  = 0100 = weak medium surfaced bouncer
5  = 0101 = weak medium surfaced dweller


What you want here, are constants set to binary values, mate. That list is pretty much nonsensical... In C that would not compile to anything. :P

Don't forget the 'b' at the end of a binary value, either.

This is how what you want to do would look:
 
const int WEAK_SM_SURFACED_BOUNCER   = 000000000001b; //weak small surfaced bouncer
const int WEAK_SM_SURFACED_DWELLER   = 000000000010b; //weak small surfaced dweller
const int WEAK_SM_BURIED_BOUNCER   = 000000000100b; //weak small buried bouncer
const int WEAK_SM_BURIED_DWELLER   = 000000001000b; //weak small buried dweller
const int WEAK_MD_SURFACED_BOUNCER   = 000000010000b; //weak med surfaced bouncer
const int WEAK_MD_SURFACED_DWELLER   = 000000100000b; //weak med surfaced dweller
const int WEAK_MD_BURIED_BOUNCER   = 000001000000b; //weak med buried bouncer
const int WEAK_MD_BURIED_DWELLER   = 000010000000b; //weak med buried dweller
const int WEAK_LG_SURFACED_BOUNCER   = 000100000000b; //weak large surfaced bouncer
const int WEAK_LG_SURFACED_DWELLER   = 001000000000b; //weak large surfaced dweller
const int WEAK_LG_BURIED_BOUNCER   = 010000000000b; //weak large buried bouncer
const int WEAK_LG_BURIED_DWELLER   = 100000000000b; //weak large buried dweller
Spoiler


Then, you can check these, or set them:
 
int val;

val |= WEAK_LG_BURIED_BOUNCER; //Sets this flag on the variable 'val'.

val &= ~WEAK_LG_SURFACED_DWELLER //Unsets the surfaced flag on it.

if ( ( val&WEAK_LG_BURIED_BOUNCER) != 0 ) //Do something if buried.
if ( ( val&WEAK_LG_SURFACED_BOUNCER) != 0 ) //Do something if surfaced.
I would split those into four flagsets, based on strength, size, status, and bounce/dwell.
 
const int ESIZE_MED = 0001b; //if not med, or lare, them it is small.
const int ESIZE_LARGE = 0010b;
const int ESTAT_BURIED = 0100b; //if not buried, then it is surfaced.
const int ESTAT_DWELL = 1000b; //If not a dweller, it must be a bouncer.

The whole point of flagsets, is to stack them in combinations that must be unique. That huge list can be simplified to four flags, that are either true, or false. Once you know which combination of flags is true, you know the resulting definition.
 
if ( ( val&ESIZE_MED ) != 0 && ( val&ESIZE_LARGE ) == 0 && ( val&ESTAT_BURIED ) == 0 && ( val&ESTAT_DWELL ) == 0 )
//It is a medium, surfaced bouncer.

Edited by ZoriaRPG, 04 January 2017 - 01:01 AM.


#4 Shadowblitz16

Shadowblitz16

    Illustrious

  • Members

Posted 04 January 2017 - 02:43 PM

so I tested this in a game maker project 

I seem to need a reverse mask

 

so here is some pseudo code..

mask[0] = 1000;
mask[1] = 0100;
mask[2] = 0010;
mask[3] = 0001;
 
shift = 0;
integer = 31;
 
//then I get a flag by..
flag_0_is_set = ((state & masks[shift]) != 0)

so I don't know what I'm doing wrong but it works

actually it doesn't work. certain bits when the integer value equal 9, 10, 12 and 13 are true even though they should be false

It works I just skrewed something up in my test project

here is the test project that I was using https://drive.google...eEVMemxkVGx6Rlk


Edited by Shadowblitz16, 04 January 2017 - 04:38 PM.



0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users