Can you tell me something about setbit()/getbit()?

  • The manual says: "setbit(x, n, b)

    Set the nth bit of x represented as a 32-bit integer to b (either 0 or 1). The resulting 32-bit integer is returned."

    But I don't understand this.

    Do you perhaps have 2 examples of when the use of setbit()/getbit() seems useful? Perhaps this gets me to understand the use of setbit/getbit.

    Thanks!

  • You can use it like a set of dip switches. instead of keeping 32 bit variables separately you can use 1 number variable and set and get the bits individually.

    In programming languages like C or something you can use boolean operators to compare numbers bitwise such as

    0101

    AND 1110

    = 0100

    but I don't think you can do that in C3. Others may correct me if I'm wrong please

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thanks, so what about this usage-examples:

    setbit(12,0,0)

    Am I correct to say here that number 12, "position 0" has value 0?

    setbit(12,0,1)

    And here, number 12, position 1, has value 1?

    But I still do not know when to use setbit().

    I hope someone knows 1 or 2 context-examples so I understand it better.

  • I mostly use it in multiplayer games when i need to network a whole bunch of booleans

    So in this event here it looks for the character being on the floor or touching a wall,

    if it is, look at virtual controller inputs and decide to mirror or not the character based on the joystick orientation and otherwise do nothing and set the variable inputs to inputs (itself)

    that way i only need to network the variable (inputs) as a 1byte and it has a bunch of information about the character such as it'S inputs and state.

    Here's another example where I do bitwise autotilling with a dictionary object

    by setting certain bits on and off it turns the variable into a number, and i can use that number to get the animation frame i want for my sprites to get this project to work drive.google.com/file/d/1ph2Ww-GcK_M1NdE57T1Ubk-YH_S9PB4R/view

    anyways, setbit and getbit are very cool, and I wish it were possible to also shift bits so I could network input buffers but I guess you can with some javascript !

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)