is there a binary type for variable?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By jinruozai

I want to save a data like : 010010010101000001000111111100011101100110101…

what if find to achive this is to use PoolByteArray.but it’s an array which means more expensive than a simple binary type.
another way is to save the data in an image,but it’s also expensive.

and the PoolByteArray’s interface is not design for binary.for example,no interface to init a size of binary with default value.(PoolByteArray can use resize,but the default value is random because it’s only resize). what’s more ,if I want to set 4~99 bit to 1,the only way is to loop and set each index,no interface like setbit(4,1,(99-4)).and so on.

so what is the right way to do this,anyone can help me,thanks a lot!

i dont get it what you want. But there is no bitsized storage type. Bits allways come in Bytes (early computers got nibbles but thats long ago). So the PoolByteArray is the way to go.

Gd Script has binary operators:
~ Bitwise NOT
<< >> Bit shifting
& Bitwise AND
^ Bitwise XOR
| Bitwise OR

with them you can manipulate the bytes on binary level

func setBit( position, value):
    var offset = floor(position / 8)
    var bitOffset = position - offset
    var byte = byteArray[offset]
    if value:
        byte = byte | (1 << bitOffset )
        byte = byte & ~(1 << bitOffset )
    byteArray[offset] = byte.

What does your setbit function should do? What are those attributes?

klaas | 2020-09-04 13:48

thank u~but what’s the type of “byte” in your code? a PoolByteArray ?

but a PoolByteArray can’t use | or & operateor

if the byte in your code is a int ,it will limit to 32bit. i want to write a large amount of bits data,a int is not enough.

i use this in my marchingcube project,to save the space points data,which is only 0 and 1.

jinruozai | 2020-09-05 07:05

Oh, im sorry i got distrected ans forgot half off the code. Byte is the byte in the array at the offset.
I would extend poolbytearray(if its possible) and add the functionality.

I have edited my answer.

klaas | 2020-09-05 11:22

thanks for your answer,I’ve got your point

jinruozai | 2020-09-07 06:03