[INFO] Bitsums and Operators
About:
This tutorial is meant for beginner scripters from amxx. This will teach you on how to use bitsum operators and show some advantages over boolean storage arrays! What is a bitsum? A bitsum is based on the way memory works. If we think, the memory chip containes cells (bits) that can hold 2 infos: 1 and 0. To store a number in one byte (byte = 8 bits) here is how to do it: "8" (normal number) = "00001000" (memory number [1 byte]) How does a bitsum work? Well the idea is that bitsums allow to control the position that we want to set it to 1. And we can check wether that position has 1 or 0. What operators do I have, and what do they mean? I will give an example for each operator: PHP Code:
Code:
| 1 0 PHP Code:
Code:
& 1 0 PHP Code:
Code:
^ 1 0 PHP Code:
Code:
~1 = 0 They are stored in the normal cells by base. So they are converted from base 10 from base 2. In this case number 9 for example becomse: "00001001". To comvert that number from the cells back to 9 we need to multiply each position with 2 and a power of it! 2^2 = power(2,2) = 4 00001001: 1*(2^0) + 0*(2^1) + 0*(2^2) + 1*(2^3) + 0*(2^4) + 0*(2^5) + 0*(2^6) + 0*(2^7) = 1 + 8 = 9 How can I easily create a bitsum? Well you need 2 operators: << and >>, or if you don't want to use those operators you can easily create one by using elements that represent powers of 2. Example: PHP Code:
"<< x" means that a position of a number is moved by x points to left! Example: PHP Code:
So if we have (1<<3) it would be like 2^3 * 1 The >> operator does the same thing like << but moves x points to right! When using integer variables the numbers disappear. PHP Code:
PHP Code:
Well it can be used for insance instead of boolean arrays. So instead of having this: PHP Code:
PHP Code:
Instead of PHP Code:
PHP Code:
PHP Code:
PHP Code:
PHP Code:
PHP Code:
Ex: PHP Code:
Hope that my english was fine! |
Re: [INFO] Bitsums and Operators
Wow thx for Great TUT!
Quote:
Code:
date: Sun Apr 19 15:20:28 2009 map: de_dust Tested on this code: PHP Code:
Now I'm thinking should I swap arrays with bitsums in my plugins code. It would use less memory, but it will be harder to understand the code. |
Re: [INFO] Bitsums and Operators
Thanks for profiling this :) and for having the patience to read it :wink:.
Indeed it will be harder to understand the code but you can also do some stocks that will make it easier to read :). |
Re: [INFO] Bitsums and Operators
Quote:
Can you give an example of such a stock? |
Re: [INFO] Bitsums and Operators
Example:
PHP Code:
PHP Code:
|
Re: [INFO] Bitsums and Operators
Macro is more appropriate here. Also you can do something that to return 0 or 1.
Code:
|
Re: [INFO] Bitsums and Operators
Quote:
PHP Code:
HTML Code:
date: Sun Apr 19 08:56:46 2009 map: de_dust2 |
Re: [INFO] Bitsums and Operators
I don't know why it's wron..
I profiled your code here is the result: Code:
date: Sun Apr 19 19:21:59 2009 map: de_dust |
Re: [INFO] Bitsums and Operators
Quote:
|
Re: [INFO] Bitsums and Operators
Anyway it is not that different when it comes to cpu power usage. It helps when we talk about memory usage.
|
All times are GMT -4. The time now is 12:42. |
Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.