0 votes

I have multiple bool varables:
var 0=1
var 1=0
var 2=0
var 3=1

I can save them as
var (int) decimal = 9

How can I find value for var 1 from decimal variable?

in Engine by (15 points)

2 Answers

+2 votes
Best answer

I think you're looking for something like this:

func _ready():
    print(is_bit_set(9, 1))
    print(is_bit_set(9, 2))
    print(is_bit_set(9, 3))
    print(is_bit_set(9, 4))

func is_bit_set(value, bit):
    return (value & (1 << (bit - 1))) > 0


by (14,938 points)
selected by
+1 vote

You can use bitwise operators. In this case use AND (&) operator. For instance decimal & 0x01 returns a value related to 1st bit , decimal & 0x02 returns a value related to 2nd, decimal & 0x04 returns a value related to 3rd bit and so on. If the returned value is 0 it means that the corresponding bit value is 0, and if the returned value is not 0 then the corresponding bit value is 1

by (24 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.