# SimpleBit - Binary Simplified [OPEN SOURCE]

## Overview

This Module allows has functions for Bit32 like bit arithmetic and bit-packing. It is currently in its early stages of development. The link for the module is here and the update log is here.

## API

Updated API is in the replies in this thread.

#### Conversion

SimpleBit.IntToBin()
``````SimpleBit.IntToBin(n:number): string
``````

Example:

``````SimpleBit.IntToBin(357)
-- output: '00000000000000000000000101100101'

``````

Description:
Returns 32 bits of an integer in string form.

SimpleBit.BinToInt()
``````SimpleBit.BinToInt(BinSequence:string): number
``````

Example:

``````SimpleBit.BinToInt('00000000000000000000000101100101')
-- output: 357
``````

Description:
Converts a binary sequence into a bit32 number.

#### Arithmetic

``````SimpleBit.Add(a:number,b:number): number
``````

Example:

``````SimpleBit.Add(81,57)
-- output: 138
``````

Description:

SimpleBit:Multiply()
``````SimpleBit.Multiply(a:number,b:number): number
``````

Example:

``````SimpleBit.Multiply(2,4)
-- output: 8
``````

Description:
Multiplies two integers together.

#### BitPacking

SimpleBit:BitPack()
``````SimpleBit:BitPack(a:number,b:number,c:number,d:number): number
``````

Example:

``````SimpleBitBitPack(129,39,41,42)
-- output: 707340161
``````

Description:
*Converts 4 numbers between 0 and 255 into an unsigned number that then could be converted back.

NOTE: Each number is given 1 byte, and since a byte can only hold numbers between 0 and 255, if any of the inputs were over that limit, the function would throw an error.*

SimpleBit:BitUnpack()
``````SimpleBit:BitUnpack(n:number): tuple
``````

Example:

``````local a, b, c, d= SimpleBit:BitUnpack(707340161)
-- output: a = 129, b = 39, c = 41,d = 42
``````

Description:
Converts a number from a `:BitPack` back into a tuple

Note: more API functions will be added, this is just the early stage of SimpleBit.
What are your ideas and feedback, iâ€™d love to hear them!

Also I recommend this tutorial if you want to know more about Bitpacking, and if you are struggling with understanding what Bit32 actually does, I recommend Robloxâ€™s Bit32 document.

14 Likes

The module is currently not for sale.

1 Like

I fixed it, it is now for sale!

1 Like

One byte can contain decimal numbers inclusively between 0 and 255. The way you worded it is a bit misleading.

2 Likes

whoops, I forgot to consider that it is `2^8-1` and not just `2^8`, turns out that the 9th bit is is worth `256`. Thanks for correcting me!

2 Likes

## 64 Bit coming soon!

I will soon add 64 bit compatability so that decimals are supported by the arithmetic system.

But as of now, the Multiplier and Adder do not take decimals in account.
But there is a way to use decimals.

Lets say you want to add 1.45 to 6.12 in the adder.
First you will have to times both numbers by 100, getting you 145 and 612,
Then you run that through the add function with `SimpleBit.Add(145,612)`, which will get you 757.
Then devide 757 by 100 and you will get your answer: 7.57.

1 Like

Not neccesarily, bits start at 0 and theres 8 bits so itâ€™s just the summation of 2^n for n being integers of range 0-7. do the math, 2^7+2^6+2^5+2^4+2^3+2^2+2^1+2^0 = 255

1 Like

## Functionality Update:

`SimpleBit.Add()` now works with a combination of bit32 `xor`, `not`, `or`, and `and` logic operations.
This reduces the time it takes to perform the function by over 90%!

This also influences `SimpleBit.Multiply()` as it works on the add function, soon I will update the multiply function too for better performace!

1 Like

I dont understand this type of stuff but, What could this be used for exactly? (just asking)

1 Like

The arithmetic like `BitSimple.Add` and `BitSimple.Multiply` work far faster than `+` or `*`.

For example, `SimpleBit.Add(50,51)` is 90%+ faster than `50+51`. True with every other integer.
The difference is slight, but when math is used a lot, it can make a difference in performance.

On the top is the time it takes to do `50+51`
On the bottom is the time it takes to do `SimpleBit.Add(50,51)`
This is a calculational error on my side, the reasons to why the the Bit32 adding method is slower have been explained lower in the comments.

And for BitPacking, it compacts 4 numbers (1 byte in lengh each) between 0 and 255 into an unsigned number (all of those bits combined) that could then be decoded back into their 4 original numbers with `SimpleBit.BitUnpack`. Saving 300%+ more Data in the process.

EDIT:
what I said about the arithmetic is complete nonsence, please ignore that.

Please use traditional `+` and `*` arithmetic instead of the `.BitMultiply` and `.BitAdd`, as my functions are more of a thing to play with and not meant for actual use.

2 Likes

Very nice module, but I just canâ€™t figure out what bitpacking is or what can be used for

1 Like

Here is an example of a usage of `SimpleBit.BitPack` and `BitUnpack`:

``````local BitpackNumber = SimpleBit.BitPack(81,49,19,49)
-- returns a number between 0 and 2^32-1

local BitUnpackResult = SimpleBit.BitUnpack(BitpackNumber)

-- what BitUnpackResult is is just  a table of the results that were packed.
-- for this example, it would be {81,49,19,49}
-- in the next update I will make it return a tuple instead of a table
-- so local a,b,c,d = SimpleBit.BitUnpack(BitpackNumber) would work

-- note
-- all the values given in have to be below 256 and 0 or above.

-- it basically compresses 4 numbers into 1 that can then be translated back``````
1 Like

I wasnâ€™t asking for examples, I was asking what can it be for and what they are

1 Like

I said what it can be used for and what they are in the example.

They basically just compress 4 numbers into 1, that can then be decompressed back into original form.
They are used for packing a lot of numbres under 256 to save Data space.

Usually used to compress RGB values

1 Like

So if I were to save huge maps, it would decrease the length of the string? If so it would be cool and useful.

So it makes 4 numbers to 1? If I invent some language to resemble strings it would â€śbitpackâ€ť it right?

1 Like

For now it just compresses integers between 0 and 255. If your MapData terrain is in intengers and each part does not get above 255 or below 0, it would help.

Sadly strings canâ€™t be represented in just a byte (as far as I know), but I will work on a string-to-binary translater

1 Like

Ah. I see, thanks for the info, I guess Iâ€™ll have to â€śkeysplitâ€ť large strings

1 Like

### Minor Change

`.BitUnpack()` now returns a tuple instead an array.

``````local n = SimpleBit.BitPack(243,12,93,10)

local r = SimpleBit.BitUnpack(n)
local a = r[1] -- 243
local b = r[2] -- 12
local c = r[3] -- 93
local d = r[4] -- 10
``````

This would be used:

``````local n = SimpleBit.BitUnpack

local a,b,c,d = SimpleBit.BitUnpack(n)
``````
1 Like

Converting strings to an unsigned number and then packing that number if possible can save data.

1 Like

Thatâ€™s what I was thinking, however Iâ€™m not sure how to make a number split.

1 Like