Hey I am just really curious on what features of the bit32 library is useful for since I don’t really get the point of it but I expect it is useful within math equations or authentication checks to see if a remote is getting fired rapidly or something like that
so a bit is like a switch can be 0 or 1
and the bit32 library lets you control 32 bits
for instance
00000000000000000000000001000101 = 69
so the 32 bits above is equal to the value 69 if we count the bits in binary
so there is a lot of things you could do with these 32 bits
for instance what you can do is store a number on the first 16 bits and a second number on the last 16 bits like so
-- the 2 numbers we want to save
local value1 = 123
local value2 = 46346
-- save both numbers into a single variable
local save = bit32.replace(value1, value2, 16, 16)
-- extract the 2 values back from the single variable
local loadedValue1 = bit32.extract(save, 0, 16)
local loadedValue2 = bit32.extract(save, 16, 16)
-- print the 2 values
print(loadedValue1)
print(loadedValue2)
so
00000000000000000000000001111011 = 123
and
00000000000000001011010100001010 = 46346
so we merge the bits together using bit32.replace
0000000001111011 | 1011010100001010
00000000011110111011010100001010 = 8107274
then we use bit32.extract(save, 0, 16)
to get the first 16 bits
and bit32.extract(save, 16, 16)
to get the last 16 bits
but this is just one example there are 1000s of things you can do with 32bits you can think of it a bit like 32 booleans
local boolean01 = false
local boolean02 = false
local boolean03 = false
local boolean04 = false
local boolean05 = false
local boolean06 = false
local boolean07 = false
local boolean08 = false
local boolean09 = false
local boolean10 = false
local boolean11 = false
local boolean12 = false
local boolean13 = false
local boolean14 = false
local boolean15 = false
local boolean16 = false
local boolean17 = false
local boolean18 = false
local boolean19 = false
local boolean20 = false
local boolean21 = false
local boolean22 = false
local boolean23 = false
local boolean24 = false
local boolean25 = false
local boolean26 = false
local boolean27 = false
local boolean28 = false
local boolean29 = false
local boolean30 = false
local boolean31 = false
local boolean32 = false
so try to imagen all the things you could do with all these booleans the possibilities are endless
Thankyou I have more understanding on it now
It usually is useful for optimization. In low level languages like C, it is almost impossible not to use bitwise operations.
Like @5uphi said, a number is basically a sequence of 32 booleans. It’s actually 64 and can store floating point numbers, but it’s better to ignore that fact and pretend it is 32 bits with the bit32
library.
Here is a program (original here) which randomly selects players using the bit32 library. I only used the simpler functions, the ones with corresponding operators in C.
local plrsserv = game:GetService("Players")
local plrs = plrsserv:GetPlayers()
local nplrs = #plrs
local choosenplrs = {}
local choosenum = math.random(bit32.lshift(1, nplrs - 1), bit32.lshift(1, nplrs) - 1)
for i = 1, nplrs do
local n = bit32.band(choosenum, bit32.lshift(1, i - 1))
if n then table.insert(choosenplrs, plrs[i]) end
end
Here is a quick explanation of the functions used here:
Left Shift a.k.a. lshift (<<
in most languages)
Left shift simply shifts the binary number to the left. For example, for a 4 bit number, 0b0010 << 2
(0b
represents a binary number) is 0b1000
. A left shift is identical to multiplying by a power of 2. In other words, x << y
= x * 2^y
.
Bitwise AND a.k.a. band (&
in most languages)
Bitwise AND is similar to and
in Lua, except it applies that to every bit. In other words, bitwise AND takes two integers, and returns a number which has the 1s wherever both numbers have ones. For example: 0b1001 & 0b1010
is equal to 0b1000
because that bit is the only bit which is 1 on both of the numbers.
By the way, I haven’t tested the code, but it should work.
The bit32
library is only compatible with signed 32-bit integers, hence its name.
The library’s documentation can be found here.
I was referring to the fact that bit32
doesn’t really do stuff with the bits literally, for example 0b10011
won’t actually be represented as 10011
.
Edit: Oh, OK. Sorry for not understanding your reply correctly.
Wasn’t a correction, just providing additional information for the thread’s poster.