SIMPLEBIT V2 - Roblox bitwise made easy!

After over a year of forgetting that I promised a V2 for the SimpleBit module that I’ve made back in early 2022.

SIMPLEBIT V2

SimpleBit.b32

includes all bit32 functions

.scramble()
SimpleBit.b32.scramble(text: string, aSeed: number, bSeed: number): string

Using a sequence of bitwise operations using the aSeed and bSeed, the output is a string, being scrambled. Changing the key will change the results.

.float32()
SimpleBit.b32.float32(n: number): number
Returns the first 32 bits of the float of the number.
.bnand()
SimpleBit.b32.bnand(n: number): number

The bitwise nand function, not present in roblox’s default bit32 library.

.packBooleans()
SimpleBit.b32.packBooleans(...:Boolean): number

Takes in a tuple of booleans, upto 32 booleans, and stores them in a number.

.unpackBooleans()
SimpleBit.b32.unpackBooleans(n: number): ...Boolean

Returns a tuple of all 32 boolean values representing the binary values, you can always use select() to chose the tuple you want.

.packColors()
SimpleBit.b32.packColors(x: {Color3}): {number}

Packs color3 values into a number. Saves space by saving R, G and B values as a single number instead of 3.

.unpackColors()
SimpleBit.b32.unpackColors(x: {number}): {Color3}

Unpacks the values of numbers into color3 values.

.random_flip()
SimpleBit.b32.random_flip(number: number, seed: number)

Performs the xor operation with the number and a random number between 0 and 2^32-1, with the seed being the randomseed used for the random generation.

.random_significant()
SimpleBit.b32.random_significant(randomseed: number?): number

Returns a random number, but each bit (index: i) has a chance of 1 / 2 ^ (i - 1) for the bit to be toggled on. I.E there is a 100% chance of getting BIT0, 50% chance of getting BIT1, ect.

SimpleBit.b32.sign_bnot() is the same as bit32.bnot, but it only performs the NOT operation to the highest bit.

SimpleBit.b64

Has commands: bor(), bnot(), band(), bnand(), bxor(), similar to its bit32 counterparts, but it can do these operations for numbers upto 18,446,744,073,709,552,000 instead of the bit32 limit of 4,294,967,295.

SimpleBit.b64.splitB2() takes in an integer, anywhere between 0 and 18,446,744,073,709,552,000, and splits it into two integers, smaller than 2^32, so that they can be proccessed by bit32 and other functions.

METATABLES

How to use the new SimpleBit metatables?

First of all, import the module to your script:
image

Use the simpleBit.new() function to turn a number into a bitobject:

image

You can use the following operations for bitwise operations:

#bitobject gets the index of the highest bit in the number (i.e how many bits the number needs)

bitobject() gets the bit32 number of the object.

bitobject ^ bitobject performs the XOR operation

bitobject + bitobject performs the AND operation

bitobject - bitobject performs the AND(a, NOT(b)), operation. Similar to XOR but any bits present in b and not in a aren’t added.

bitobject > bitobject shifts the first b.o by the second b.o to the right.

bitobject < bitobject shifts the first b.o by the second b.o to the left.

bitobject >= bitobject rotates the first b.o by the second b.o to the left.

bitobject <= bitobject rotates the first b.o by the second b.o to the right.

-bitobject returns the INVERSE of bitobject.

bitobject * bitobject performs the OR OPERATION

NOTE:
Apart from the operator functions (<, >, <=, >=), all these functions return the result for the operation and do not change the actual bitobject, whilst the operator functions actually modify the bitobject.

SIMPLEBIT V2 MODULE

11 Likes

Updated .scramble() to use a more complex method, where the size of the string inputted is used as a third seed.

Poll time!

Do you intend to use the module?

  • I intend to use SBV2 in my games
  • I intend to use SBV2 for certain cases
  • I intend to use SBV2 to play around with the module
  • I don’t intend to use the module

0 voters

Do you think SimpleBit V2 is a step up from SimpleBit V1?

  • Yes
  • SimpleBit V1 is better in some aspects, but V2 is better overall
  • Never used SimpleBit V1

0 voters

Do you think that the metamethods are assigned correctly?

By metamethods I’m reffering to +, -, *, / ect. that are used between SimpleBit instances

  • Yes
  • No
  • No opinion

0 voters

Did SimpleBit help you with the bit32 library / learning bit manipulation?

  • Yes, it helped me
  • No, because I already knew those things
  • No, I still don’t understand those things

0 voters

This was something I was looking for, thanks for bumping lol

2 Likes

Will soon add binary shifting and binary rotating to b64 values, after I figure out how to implement it whilst keeping the conversion fast.

Decimal support coming soon

Going to add a 2-part (64b) method to be able to represent 20b of decimal and 44b of integer, although the bitsize for each part is still up to change.