What is Bit32 how does it work?

I’ve been seeing this thing called Bit32 and I see that it uses like different operators like & and | I asked AI to explain but I’m a little dumb I also looked at this but didn’t really understand it so I’m wondering if any smart people could help me out?

It just contains functions for bitwise operations. You probably aren’t gonna need this often, but u should do ur own research to understand how they work.

I understand that but I’m wondering if someone could explain what the bitwise operators are and how they work like whats 1 & 1

You can think of 1 as true and 0 as false. What’s true and true in lua? it’s true, so therefore 1. Here’s a website I found that might help with the rest of the operations Bitwise Operations - Systems Encyclopedia

Never.

Roblox has killed this poor library entirely.
Sadly you don’t have much use of it anymore if ever and this library only works only on server now.
Maybe in the far future roblox will “fix” this library so we can store booleans in a more optimized way :money_mouth_face:

1 Like

What are some uses you’ve seen for the library on Roblox? I can think of bit manipulation for efficiency things outside of Roblox, like bitmask DP or something like parity checking in Python.

This straight up isn’t true at all, lol. Here’s a string hashing module that works both on the client and the server.

return function(str: string): number
        local h = 5381
        for c in str:gmatch(".") do
                h = math.fmod((bit32.lshift(h, 5) + h) + string.byte(c), 2147483648)
        end
        return h
end
1 Like

That cause limitation has been lifted most likely.
I remember it being server only and erroring on a client.

Bitwise operators are really low-level. If you know some binary (e.g. 0001 = 1, 0010 = 2, 0011 = 3, so on and so on), it can be of some use to you. Something like bitwise flags are an efficient way of storing multiple states in one integer; Roblox has a function called bit32.btest which is particularly useful for that case; it checks if bit32.band(a, ...) is not equal to 0.

The & symbol is the “AND” operator. From Mozilla’s documentation:

The bitwise AND (&) operator returns a number or BigInt whose binary representation has a 1 in each bit position for which the corresponding bits of both operands are 1.

It also has a little test case that you can run to grasp how it works. bit32.band is also used for bitmasking; you’ll have to look into that in your own time, should you need it for your project. Generally, the bit32 library is great for serdes if you know how to use it efficiently

What?

I use the bit32 library for bitwise flags all the time, both on the server and the client. Where did you get the information that it only works on the server???

1 Like

You should learn binary first, this will make it easier to understad what bitwise operations do.

To answer your actual question about 1 & 1: this is a bitwise AND, explicitly treating the numbers 1 and 1 as sequences of bits. Bitwise AND behaves differently from logical AND, which is written as ‘&&’ in some programming languages, hence the use of just one ‘&’ to make the distinction. (The same is true of bitwise OR ‘|’ and logical OR ‘||’.)

To understand what 1 & 1 will do, it helps to think in terms of something called a truth table, which tells you what value you get when you operate on bits. The truth table looks like this for AND:

a | b | z
0 | 0 | 0
0 | 1 | 0
1 | 0 | 0
1 | 1 | 1

where a and b are the inputs and z is the output. In other words, the result is 1 if both bits are 1, otherwise it’s 0. “Bitwise AND” means we apply the AND operation bit by bit (i.e., bitwise), just like how we might add two numbers together digit by digit. If we have two 32-bit numbers 1 and 1, then, we’d AND them like this:

0 0 … 0 0 1
& 0 0 … 0 0 1
= 0 0 … 0 0 1

where there are 31 zeroes before each 1. So, 1 & 1 is just 1.

This kind of thing can be useful if you’re using a sequence of bits to store flags, for example. Suppose I have a variable flags where the least significant bit (rightmost bit) of the variable means “valid.” Then what I’m saying is that that bit will be set (equal to 1) if some associated data is valid, and unset (equal to 0) otherwise. Then I can check if the data is valid by checking if flags & 1 is equal to 1. This is because, no matter what other bits are set in flags, AND’ing it with 1 will result in a number that is either 0 if the valid bit is 0 or 1 if the valid bit is 1.

If you understood the above, you can also understand bitwise OR ‘|’, NOR (sometimes ‘~’), and XOR (sometimes ‘^’) just by replacing the truth table for AND with the corresponding truth tables of those operators.

thanks I have a lot better understanding of it now

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.