AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   [INFO] Bitsums and Operators (https://forums.alliedmods.net/showthread.php?t=90507)

joaquimandrade 05-24-2009 10:09

Re: [INFO] Bitsums and Operators
 
1 Attachment(s)
I also confirm that it works. (And also, thanks Damizean)

The improvement:
PHP Code:

dateSun May 24 06:58:56 2009 mapde_dust2
type 
|                             name |      calls time min max
-------------------------------------------------------------------
   
|                       server_cmd |          0.000014 0.000014 0.000014
   p 
|                       plugin_cfg |          0.041587 0.041587 0.041587
   f 
|                           oldWay |      10000 0.025170 0.000001 0.000081
   f 
|                           newWay |      10000 0.024912 0.000001 0.000103
0 natives
public callbacksfunction calls were not executed


Exolent[jNr] 05-24-2009 12:56

Re: [INFO] Bitsums and Operators
 
I understand the "x / 32" -> "x >> 5" because like you said, bitshifting is a power of 2.
x * power(2, y) = x << y
x / power(2, y) = x >> y

However, I don't understand why "x % 32" was changed to "x & 31".
I know that the & operator compares the bits, but it just doesn't make sense to me.

joaquimandrade 05-24-2009 13:19

Re: [INFO] Bitsums and Operators
 
Quote:

Originally Posted by Exolent[jNr] (Post 833769)
I understand the "x / 32" -> "x >> 5" because like you said, bitshifting is a power of 2.
x * power(2, y) = x << y
x / power(2, y) = x >> y

However, I don't understand why "x % 32" was changed to "x & 31".
I know that the & operator compares the bits, but it just doesn't make sense to me.

x & 31 will translate always to a value between 0 and 31. That's part is obvious (And that's what we want). Then, the less obvious, is how the result value "cycles":

When x is a power of 2 bigger or equal to 32 (i.e. 32,64,128,...) , (x & 31) is 0

Code:

(32)
  0000011111
& 0000100000

(64)
  0000011111
& 0001000000

(128)
  0000011111
& 0010000000

When x grows after that, (x & 31) will grow equally until be again a power of 2.

Code:

(33) => (33 & 31) == 1
  0000011111
& 0000100001

(34) => (34 & 31) == 2
  0000011111
& 0000100010

(35) => (35 & 31) == 3
  0000011111
& 0000100011

...

(63) => (63 & 31) == 31
  0000011111
& 0000111111

(64) => (64 & 31) == 0
  0000011111
& 0001000000

So, basically it goes:

0 .. 31 , 0 ... 31

Exolent[jNr] 05-24-2009 13:41

Re: [INFO] Bitsums and Operators
 
I pretty much understand it now, thanks.
I guess I need to brush up on my knowledge of bits.

joaquimandrade 05-24-2009 13:43

Re: [INFO] Bitsums and Operators
 
Quote:

Originally Posted by Exolent[jNr] (Post 833804)
I pretty much understand it now, thanks.
I guess I need to brush up on my knowledge of bits.

I guess that this is pretty much advanced stuff. Probably this guy has some special knowledge on this topic

Bugsy 05-24-2009 14:31

Re: [INFO] Bitsums and Operators
 
Here's a few examples of what is taking place:

Code:

34 & 31 = 2 === 34 % 32 = 2

The only matching bit (&) out of these two numbers is bit 2 which represents 2.
34 = 100010
31 = 011111
02 = 000010  =  Result of 34 & 31

34 modulus 32 = 2

Code:

19 & 31 = 19 === 19 % 32 = 19

The bits that match up in these two numbers represents 19.
19 = 10011
31 = 11111
19 = 10011  =  Result of 19 & 31

19 modulus 32 = 19

Code:

32 & 31 = 0 === 32 % 32 = 0

Notice that no bits match so the result of this is 0
32 = 100000
31 = 011111
00 = 000000  == Result of 32 & 31

32 modulus 32 = 0

Here's a little web tool you can use to play with binary <-> decimal.
http://www.openstrike.co.uk/cgi-bin/decimalbinary.cgi

Here's a printout of the values for 0 - 34 comparing ( x & 31 ) and ( x % 32 )
Code:

0 & 31 = 0 === 0 % 32 = 0
1 & 31 = 1 === 1 % 32 = 1
2 & 31 = 2 === 2 % 32 = 2
3 & 31 = 3 === 3 % 32 = 3
4 & 31 = 4 === 4 % 32 = 4
5 & 31 = 5 === 5 % 32 = 5
6 & 31 = 6 === 6 % 32 = 6
7 & 31 = 7 === 7 % 32 = 7
8 & 31 = 8 === 8 % 32 = 8
9 & 31 = 9 === 9 % 32 = 9
10 & 31 = 10 === 10 % 32 = 10
11 & 31 = 11 === 11 % 32 = 11
12 & 31 = 12 === 12 % 32 = 12
13 & 31 = 13 === 13 % 32 = 13
14 & 31 = 14 === 14 % 32 = 14
15 & 31 = 15 === 15 % 32 = 15
16 & 31 = 16 === 16 % 32 = 16
17 & 31 = 17 === 17 % 32 = 17
18 & 31 = 18 === 18 % 32 = 18
19 & 31 = 19 === 19 % 32 = 19
20 & 31 = 20 === 20 % 32 = 20
21 & 31 = 21 === 21 % 32 = 21
22 & 31 = 22 === 22 % 32 = 22
23 & 31 = 23 === 23 % 32 = 23
24 & 31 = 24 === 24 % 32 = 24
25 & 31 = 25 === 25 % 32 = 25
26 & 31 = 26 === 26 % 32 = 26
27 & 31 = 27 === 27 % 32 = 27
28 & 31 = 28 === 28 % 32 = 28
29 & 31 = 29 === 29 % 32 = 29
30 & 31 = 30 === 30 % 32 = 30
31 & 31 = 31 === 31 % 32 = 31
32 & 31 = 0 === 32 % 32 = 0
33 & 31 = 1 === 33 % 32 = 1
34 & 31 = 2 === 34 % 32 = 2


Exolent[jNr] 05-24-2009 14:38

Re: [INFO] Bitsums and Operators
 
Okay, I understand it now.
I was looking at it in a totally wrong way.

stupok 05-24-2009 16:06

Re: [INFO] Bitsums and Operators
 
It didn't get through my thick skull until I saw this:

Quote:

Originally Posted by Bugsy (Post 833841)
Code:

34 & 31 = 2 === 34 % 32 = 2

The only matching bit (&) out of these two numbers is bit 2 which represents 2.
34 = 100010
31 = 011111
02 = 000010  =  Result of 34 & 31

34 modulus 32 = 2


Thanks for taking the time to explain this stuff, guys :up:

jim_yang 07-09-2009 02:58

Re: [INFO] Bitsums and Operators
 
I recommend a good article about bits
http://www-graphics.stanford.edu/~seander/bithacks.html

claudiuhks 10-26-2013 16:20

Re: [INFO] Bitsums and Operators
 
Are you sure the next expressions are true, ot_207?

PHP Code:

~0
~



All times are GMT -4. The time now is 14:36.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.