This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Sewbacca's profile picture

Bit API

Started by Sewbacca, 20 April 2016 - 02:40 PM
Sewbacca #1
Posted 20 April 2016 - 04:40 PM
http://www.computercraft.info/wiki/Bit_(API)

I don't get it, what does the bit API?
And what menas OR, NOT and AND?
Anavrins #2
Posted 20 April 2016 - 05:24 PM
bit and bit32 apis performs bitwise operation on 32bits number.
https://en.wikipedia.org/wiki/Bitwise_operation
Sewbacca #3
Posted 20 April 2016 - 07:12 PM
I just understand nothing. bit.bnot(1100) gives me : 456256256 or something like that, not 0011.
Edited on 20 April 2016 - 05:12 PM
Dragon53535 #4
Posted 20 April 2016 - 08:33 PM
Because it converts the base ten 1100 (not binary 1100) into a 32 bit number, and then inverts that, and returns you the base 10 equivalent.

Bnot on wiki shows example.
Edited on 20 April 2016 - 06:33 PM
KingofGamesYami #5
Posted 20 April 2016 - 08:33 PM
Of course it does. The binary representation of 1100 is

010001001100
After using bnot, it would be

101110110011

Which is, of course, equivalent to the integer you saw.
Dragon53535 #6
Posted 20 April 2016 - 08:37 PM
Of course it does. The binary representation of 1100 is

010001001100
After using bnot, it would be

101110110011

Which is, of course, equivalent to the integer you saw.

Sort of right, but bnot forces 32bit. so what he "technically" inputted was


00000000000000000000010001001100

And of course the inverse of that is:


11111111111111111111101110110011
Sewbacca #7
Posted 20 April 2016 - 10:14 PM
Of course it does. The binary representation of 1100 is

010001001100
After using bnot, it would be

101110110011

Which is, of course, equivalent to the integer you saw.
I got 2883900787 after using bit.bnot(010001001100).
Lyqyd #8
Posted 20 April 2016 - 10:17 PM
I don't think you're realizing that there's a significant difference between binary 1100 and decimal 1100 and that the way Lua interprets numbers doesn't change just because you're calling a function in the bit library.
Dragon53535 #9
Posted 21 April 2016 - 01:20 AM
As Lyqyd said, the bnot function uses a base 10 number. Even if you use only 0's and 1's it's still counting as a base 10 number. 1100 is one thousand one hundred, not 12. It takes that thousand one hundred and converts it into base 2, 010001001100, the thing is that bnot works specifically on 32 bit integers, so what it does is tack on 20 0's onto the front of that number and viola you get, 00000000000000000000010001001100. It then of course does a binary not on that, which you already know how that works, and it give back 11111111111111111111101110110011, your mystery number of 4294966195.

If you want to make it work like you want, then you either want to specifically just loop through the number passed as a string, and invert each character yourself, or you want to specifically bnot and grab only the bits that you care about.

If you had noticed in the wiki link I put, the example there shows bnot(18) which gave back a large number like yours.
Edited on 20 April 2016 - 11:21 PM
Anavrins #10
Posted 21 April 2016 - 02:01 AM
Plus if you want to have the answer as if it was an 8 bit number, you could then use the and operation to "mask" the unneeded bits, and only get the 8 least significant bits.

local n = 10 – 1010
local mask = 255 – 11111111
bit.bnot(n) – 11111111111111111111111111110101
bit.band(bit.bnot(n), mask) – 11111111111111111111111111110101
Edited on 21 April 2016 - 12:14 AM