Results 1 to 3 of 3

Thread: ClientAdmin rights & binary flags

  1. #1
    Developer Sor's Avatar
    Join Date
    Aug 2010
    The Medieval City of Bruges

    Default ClientAdmin rights & binary flags

    Binary flags are used by MoHAA's dmflags cvar and reborn's ClientAdmin rights. If you've used Linux, it is comparable with using octals for
    the 'chmod' command. Most people understand how to use them but have no clue how it works, hence why some people are unsure of
    what to do.

    I'd like to take the opportunity to clarify the principle so there will be less confusion and people will use them more willingly. Maybe this
    might inspire future mods that aim for customization.

    Binary flags are an elegant solution to an otherwise bloated sequence of options for a single command or component. The end-user experience
    usually boils down to adding specific numbers together to combine/activate the options. The main confusion is obviously caused by the
    question: "How does one extrapolate the individual numbers that were used to create the new number?" in other words, how does Razo do it?

    The answer to this question requires a basic understanding of how decimal numbers are represented in binary (specifically, the integer format).

    Binary number system

    An Nth number system is a number system that employs N number of digits (i.e. symbols used to represent one unique value). In our decimal
    number system we use 0 to 9 so 10 digits and in binary we use 0 and 1 which are 2 digits. N is the base of our number system.

    "12" is decimal number and not digit, written in the positional notation where subsequent digits, including equal digits, have a different interpreted value.
    The 0th position is the first position left of the (written or unwritten) comma. Positions left of the comma are positive and will increase by 1;
    positions right of the comma are negative and will decrease by 1. This position number is actually an exponent. Hence they can be re-written
    in their base form:
    Position: 1  0
    Number:   1  2
    Base: 10
    12 = (1 * 10^1) + (2 * 10^0)
    12 = 10 + 2
    The exact same rules apply to the binary number system. Consider this binary sequence in positional notation:
    Pos:  3 2 1 0
    Num:  1 0 1 0
    If we write the base form in our decimal number system we get the decimal value:
    Base: 2
    1010 = (1 * 2^3) + (0 * 2^2) + (1 * 2^1) + (0 * 2^0)
    1010 = (1 * 8) + (0 * 4) + (1 * 2) + (0 * 1)
    1010 =  8 + 2
    1010 = 10

    Binary flags

    The uniqueness of having only 2 digits, 1 and 0, is that only powers of the base (=2) are (1) or aren't (0) represented per position.
    (Note: a 'bit' is actually an acronym for the term 'binary digit'.)
    Pos:   7     6     5     4     3     2     1     0 
    Bin:   1     0     1     1     0     0     0     1
    Pow: 128    64    32    16     8     4     2     1
    Programmers usually only remember the powers associated with the position. In this case we get:
    128 + 32 + 16 + 1 = 177
    If you haven't noticed already, the only numbers with meaning, the only numbers you will add together for the binary flags, are also
    powers of two. The consequence is that your combined number will be represented in binary in the same way as demonstrated above:
    the positional notation (note: only for integer formats though).

    In other words each option is associated with a power of two and each power of two is represented by exactly one bit on one
    unique position. If the option/power of two was added to the endresult, its corresponding bit will be 1, otherwise the bit will be 0.

    This only works well for positive numbers (cf. signed (and unsigned) integers). But I think you understand that this association is
    really convenient. Not to mention that the symbols for representing the option are 1 and 0 which are also boolean numbers and
    they are commonly used to represent true/on/activated... and false/off/deactivated... respectively.


    The question now becomes "How can we tell which bit/option is set to 1?" The answer lies with the bitwise AND-operation.
    The AND-operation is a binary operation, contrary to the complement-operation which a unitary operation, which means it
    needs 2 operands to work. Say we have a number 184 and we want to know if the option associated with 8 is set, then
    we'll apply the AND operation on these values and check the result. In integer format this time (32 bits):
    184 = 0000 0000 0000 0000 0000 0000 1011 1000
    8   = 0000 0000 0000 0000 0000 0000 0000 1000
    Now we apply the AND-operation:
            0000 0000 0000 0000 0000 0000 1011 1000
            0000 0000 0000 0000 0000 0000 0000 1000
            0000 0000 0000 0000 0000 0000 0000 1000
    The AND-operation checks each bit at the same position in each number and will only return a 1-bit if both bits were 1.
    Here we get the number (8) that we were looking for.

    Note that bitwise operations are generally faster than any arithmetic operations. Even in MoHAA, modders
    can actually use this operation:
    local.result = 184 & 8;
    println local.result;
    The console will output 8.

    You can take this even further and test for multiple options simultaneously just by adding the to-be-checked numbers together and seeing if the result of AND is equal to it.

    I hope this is of help to someone.
    Last edited by Sor; April 5th, 2013 at 06:33 PM.
    Morpheus Script (MoH) => You try to shoot yourself in the foot only to discover that MorpheusScript already shot your foot for you.

  2. #2
    Über Prodigy & Developer Razo[R]apiD's Avatar
    Join Date
    May 2010
    Poland, Lublin


    Great tutorial

  3. #3
    Administrator James's Avatar
    Join Date
    May 2010
    on the intraweb


    Heck yeah! Nice share man!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts