Topic was automatically imported from the old Question2Answer platform.
Asked By
LDTK
Suppose that the binary form of var a is: var a = 0b11101101
How can I check and receive the state of a particular bit as output?
Example: Imagine there is a operation “state” that outputs the nth digit of the variable a:
Prints the 3rd digit of the number. (In this case, 1)
Sorry for the convoluted question
Out of curiosity, what’s your end goal here? If your goal is to check a list of bit flags for a particular flag, this can be achieved using bitwise operators like & (AND), | (OR) and ^ (XOR).
I have tried the obvious, i.e., converting a to a string. However, String(a) returns “237” and not “0b11101101”. So it does not work.
A workaround could be, if it is possible in your project, to define a as a String (var a = 0b11101101) and then get the value at a particular position by just calling a[n].
It sounds like you’re trying to check for the presence of specific flags in a bitfield.
Try something like this instead, which is more readable:
extends Node2D
# The `<<` operators performs a bit shift to the left, which can be used to elevate a number to the power of the righthand side quickly (when used with `1` on the lefthand side).
const FLAG_FALLING = 1 << 0 # 1
const FLAG_FLAMMABLE = 1 << 1 # 2
const FLAG_SLIPPERY = 1 << 2 # 4
const FLAG_NONSOLID = 1 << 3 # 8
func _ready() -> void:
# The bitwise `|` operator can be used to combine flags safely.
# `+` can also be used, but it's less safe as it won't prevent you from adding the same flag multiple times (which is invalid).
var block_flags = FLAG_FALLING | FLAG_SLIPPERY
print("Falling: ", has_flag(block_flags, FLAG_FALLING))
print("Flammable: ", has_flag(block_flags, FLAG_FLAMMABLE))
print("Slippery: ", has_flag(block_flags, FLAG_SLIPPERY))
print("Nonsolid: ", has_flag(block_flags, FLAG_NONSOLID))
func has_flag(bitfield, flag):
# The bitwise `&` operator will return a number with all bits "in common" between the two numbers set to `1`, and all other bits set to `0`. If no flags match, the returned number will be `0` since there will be no bits in common between the two numbers.
# Here, we convert it to a boolean since we are only interested in whether
return bool(bitfield & flag)