Jump to content




Bit API


  • You cannot reply to this topic
9 replies to this topic

#1 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 20 April 2016 - 03:40 PM

http://www.computerc...o/wiki/Bit_(API)

I don't get it, what does the bit API?
And what menas OR, NOT and AND?

#2 Anavrins

  • Members
  • 775 posts

Posted 20 April 2016 - 04:24 PM

bit and bit32 apis performs bitwise operation on 32bits number.
https://en.wikipedia...twise_operation

#3 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 20 April 2016 - 06:12 PM

I just understand nothing. bit.bnot(1100) gives me : 456256256 or something like that, not 0011.

Edited by Sewbacca, 20 April 2016 - 06:12 PM.


#4 Dragon53535

  • Members
  • 973 posts
  • LocationIn the Matrix

Posted 20 April 2016 - 07: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 by Dragon53535, 20 April 2016 - 07:33 PM.


#5 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 20 April 2016 - 07: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.

#6 Dragon53535

  • Members
  • 973 posts
  • LocationIn the Matrix

Posted 20 April 2016 - 07:37 PM

View PostKingofGamesYami, on 20 April 2016 - 07:33 PM, said:

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


#7 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 20 April 2016 - 09:14 PM

View PostKingofGamesYami, on 20 April 2016 - 07:33 PM, said:

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).

#8 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 20 April 2016 - 09: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.

#9 Dragon53535

  • Members
  • 973 posts
  • LocationIn the Matrix

Posted 21 April 2016 - 12: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 by Dragon53535, 21 April 2016 - 12:21 AM.


#10 Anavrins

  • Members
  • 775 posts

Posted 21 April 2016 - 01: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 by Anavrins, 21 April 2016 - 01:14 AM.






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users