How can I work with binary numbers?

rjoshuarjoshua Posts: 37Member

Hi,
I'm trying to manage the collision layer and collision mask, but, I notice that they are binary numbers.

So, I think, I need a variable that increases its value in power of 2, for example 1, 2, 4, 8, 16, 32 and so on, and beside,that can has a limit, for example, 1, 2, 4, 1, 2, 4, 1, 2, 4 etc.

I know Godot can work with hexadecimal numbers but I don't found some example of binary numbers.

How can I make what I want to do? Or what is the right way to make this?


Tags :

Best Answers

  • rjoshuarjoshua Posts: 37
    Accepted Answer

    Finally, I found a answer. We can deal with binary numbers as if they were string, with these functions:

    func _ready():
    
        var b = "1111011"
        var i = bin2dec(b)
        print(i) # 123
    
    # Takes in a decimal value (int) and returns the binary value (int)
    func dec2bin(var decimal_value):
        var binary_string = "" 
        var temp 
        var count = 31 # Checking up to 32 bits 
    
        while(count >= 0):
            temp = decimal_value >> count 
            if(temp & 1):
                binary_string = binary_string + "1"
            else:
                binary_string = binary_string + "0"
            count -= 1
    
        return int(binary_string)
    
    # Takes in a binary value (int) and returns the decimal value (int)
    func bin2dec(var binary_value):
        var decimal_value = 0
        var count = 0
        var temp
    
        while(binary_value != 0):
            temp = binary_value % 10
            binary_value /= 10
            decimal_value += temp * pow(2, count)
            count += 1
    
        return decimal_value
    

    This code was made by IVAN SKODJE, he explain it in his blog: ivanskodje.com/2016/10/11/55/

  • rjoshuarjoshua Posts: 37
    Accepted Answer

    @GerHarm I will explain it:
    phisicsobjects such as kinematicBody, rigidBody and staticBody have a property called collision_layer and collision_mask, and their values are binary.

    What I wanted to do was change the value of these variables in order, something like decimal numbers: 1, then 2, 3, 4, 5. But since they are binary, in decimal their value would be 1, 2, 4, 8, 16 etc.

    image

    However when I read your question, I remember the binary operators. I have completely forgotten them! but rather as I saw that gdscript had not incorporated binary numbers, maybe I assumed that binary operator did not exist. :/

    The point is that I found a way (thanks to you) to do what I wanted to do, assign the value of these variables as if they were decimals:

    extends KinematicBody2D
    
    func _ready():
        set_layer(4)
        print(self.collision_layer) # print 8
        set_layer(5)
        print(self.collision_layer) # print 16
    
    func set_layer(n):
        self.collision_layer = 1 << (n-1)
    

    Thank you! :)

Answers

  • rjoshuarjoshua Posts: 37Member
    Accepted Answer

    Finally, I found a answer. We can deal with binary numbers as if they were string, with these functions:

    func _ready():
    
        var b = "1111011"
        var i = bin2dec(b)
        print(i) # 123
    
    # Takes in a decimal value (int) and returns the binary value (int)
    func dec2bin(var decimal_value):
        var binary_string = "" 
        var temp 
        var count = 31 # Checking up to 32 bits 
    
        while(count >= 0):
            temp = decimal_value >> count 
            if(temp & 1):
                binary_string = binary_string + "1"
            else:
                binary_string = binary_string + "0"
            count -= 1
    
        return int(binary_string)
    
    # Takes in a binary value (int) and returns the decimal value (int)
    func bin2dec(var binary_value):
        var decimal_value = 0
        var count = 0
        var temp
    
        while(binary_value != 0):
            temp = binary_value % 10
            binary_value /= 10
            decimal_value += temp * pow(2, count)
            count += 1
    
        return decimal_value
    

    This code was made by IVAN SKODJE, he explain it in his blog: ivanskodje.com/2016/10/11/55/

  • SomnivoreSomnivore Posts: 105Member

    This looks like prime feature request material, I imagine such a helper function should be built-in to GDScript

  • GerHarmGerHarm Posts: 2Member

    I don't understand the question, i think.
    What exactly are you trying to do? what's wrong with using the bitwise operators like everybody else? or is this just for printout?

  • rjoshuarjoshua Posts: 37Member
    Accepted Answer

    @GerHarm I will explain it:
    phisicsobjects such as kinematicBody, rigidBody and staticBody have a property called collision_layer and collision_mask, and their values are binary.

    What I wanted to do was change the value of these variables in order, something like decimal numbers: 1, then 2, 3, 4, 5. But since they are binary, in decimal their value would be 1, 2, 4, 8, 16 etc.

    image

    However when I read your question, I remember the binary operators. I have completely forgotten them! but rather as I saw that gdscript had not incorporated binary numbers, maybe I assumed that binary operator did not exist. :/

    The point is that I found a way (thanks to you) to do what I wanted to do, assign the value of these variables as if they were decimals:

    extends KinematicBody2D
    
    func _ready():
        set_layer(4)
        print(self.collision_layer) # print 8
        set_layer(5)
        print(self.collision_layer) # print 16
    
    func set_layer(n):
        self.collision_layer = 1 << (n-1)
    

    Thank you! :)

  • GerHarmGerHarm Posts: 2Member

    That's what i was thinking. Btw i just checked the API and the base class KinematicBody2D already has methods for this:

    bool get_collision_layer_bit ( int bit ) const
    bool get_collision_mask_bit ( int bit ) const
    void set_collision_layer_bit ( int bit, bool value )
    void set_collision_mask_bit ( int bit, bool value )

    Cheers,

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file