Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

operators

Comments in 'Plugin Development' started by Creeperface, Oct 25, 2015.

  1. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    how to use these operators?

    from pocketmine:
    PHP:
    $this->sneaking = (($flags 0x40) > 0);
    PHP:
    $flags |= 0x100;
    PHP:
    return PHP_INT_SIZE === ? (($x 0xFFFFFFF) << 35) | (($y 0x7f) << 28) | ($z 0xFFFFFFF) : $x ":" $y .":"$z;
    //mean "|" operator
  2. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    Before creating a thread, why not look at the PHP manual? o_O PS: They are called bitwise operators, learn about them here: http://php.net/manual/en/language.operators.bitwise.php
    Creeperface likes this.
  3. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    0b10101010 means 10101010 (binary form). 0xABCD means ABCD (hexadecimal form). So, if you know what binary and hexadecimal are, you know that 0xFF = 0x100 - 1 = 256 - 1 = 255

    These are bitwise operators.

    First of all, to make it easier for thinking, convert them all into binary form.
    E.g.
    PHP:
    124 63
    In binary form:
    PHP:
    0b1111100 0b111111
    As anyone with basic knowledge about how computers work should know, modern computers work by calculating things in binary form, and each digit is one bit.
    Now, we compare the two numbers bit by bit.
    Code:
    1111100
    0111111 (we add the leading 0 to align the numbers)
    
    Now, let's think, what does & mean? It means "and" in common English. And what does "and" mean? It means, if front and back is true, it outputs true. If front and back are both false, or one is true and one is false, it outputs false.
    Therefore, 1&1 outputs 1, 1&0 outputs 0, 0&1 outputs 0, and 0&0 outputs 0.

    When there is more than one bit, it is compared bit by bit:
    Code:
    0 1 1 1 1 1 0 0
    0 0 1 1 1 1 1 1
    = = = = = = = =
    0 0 1 1 1 1 0 0
    (I added the leading zeroes just to let you know 0&0 is 0; it is the same and not necessary)

    What you would observe is that by doing & 0b111111 (= 63 = 0x3F), we are actually setting all the bits of the original number to 0 except the last 6 bits should keep their original value. In another way, you can consider it as taking the remainder of the original number being divided by 0b1000000 (= 64 = 0x40). So 63&63 = 127&63 = 63, 1&64 = 1

    |, as you already know from ||, means or.
    Don't confuse: || is a logical operator that compares booleans (true/false); | is a bitwise operator that compares integers (or strings, but the strings will be compared bit by bit as if they are numbers as if they are ord()'ed, but you usually don't need to bitwise compare strings unless you are doing cryptography things).
    | is same as &, except it is an or operator.
    Code:
    0|0 => 0
    0|1 => 1
    1|0 => 1
    1|1 => 1
    
    So for example:
    0x0F | 0x80 = 0x8F

    << and >> are bit shifting. You can consider as multiplying and/or dividing powers of 2.
    0b1011 >> 2 = 0b10 (the last two bits, 11, are shifted away, and the other bits are shifted rightwards by two) equivalent to dividing by 4 except that it is faster.
    0b10 << 2 = 0b1000 (adds two 0 bits at the end and shift the original bits leftwards by two)

    So when we, for example, want to combine three bytes (R, G, B) into one integer:
    ( (R & 0xFF) << 16 ) | ( (G & 0xFF) << 8 ) | (B & 0xFF)
    8 is the size of a byte, so in the output integer, the smallest 8 bytes are about B, the next 8 about G and the next 8 about R. So for example, the color #64FF00 (R=0x64, G=0xFF, B=0x00) would output as 0x64FF00. (@64FF00 I know you are sneezing)

    PHP_INT_SIZE returns the system's architecture, e.g. 32-bit, 64-bit, etc. (Actually, PocketMine only supports 64-bit and 32-bit, so no need to worry about other architectures) PocketMine is checking it because it wants to know if it can store two 32-bit integers together.
    That method is used for fast chunk indexing. In PHP, arrays with integers as keys are faster than arrays with strings as keys.
    The ordee of a chunk is expected to be two 32-bit signed integers (X and Z). If you are on a 64-bit system, you can do (X << 32) | Z, which will become a 64-bit-long number.
    However, in 32-bit systems, since the bits out of bounds at the left will get deleted, doing << 32 on a number will make it 0. Therefore PocketMine uses strings as keys on 32-bit systems. This is also why 64-bit is faster than 32-bit :)

    Note that bitwise OR and bitwise AND are not directly inverse operations. However, OR is partial inverse of NAND, and NOR is partial inverse of AND.
    For example, | 0x80 sets the 8th smallest bit of the number, and &~ 0x80 unsets that bit (~ is NOT, which toggles all the bits of the number between 0 and 1, so &~ is NAND). However, obviously, you can't reverse the number with this.

    I just noticed I spent a whole hour to type this on a mobile o_O

    Please leave a like if you find this helpful :)

    What if he doesn't understand the PHP manual? And mind explaining more, don't you]
    Last edited: Oct 25, 2015
  5. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    thx a lot :)
  6. EvolSoft
    Offline

    EvolSoft Notable Member Plugin Developer

    Joined:
    Sep 10, 2014
    Posts:
    821
    Plugins:
    15
    Minecraft User:
    Flavius12
    This reminds me when I was working on a FAT FileSystem library in C when I had to format the last modification date of a file :p
  7. GlaciercreepsMC
    Offline

    GlaciercreepsMC Active Member Plugin Developer

    Joined:
    Jan 21, 2014
    Posts:
    91
    Plugins:
    1
    Minecraft User:
    GlaciercreepsMC
  8. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    if you learn C and how computer works, you will know that too
  9. GlaciercreepsMC
    Offline

    GlaciercreepsMC Active Member Plugin Developer

    Joined:
    Jan 21, 2014
    Posts:
    91
    Plugins:
    1
    Minecraft User:
    GlaciercreepsMC
    I've learned both, I've just never known how to use bitwise operators and why we should use them. I know hex numbers are close to binary digits but I've never had to use neither bitwise operators nor hex numbers.
    Legoboy0215 likes this.
  10. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    i think basic programming languages use only this
  11. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    You mean machine languages?
  12. EvolSoft
    Offline

    EvolSoft Notable Member Plugin Developer

    Joined:
    Sep 10, 2014
    Posts:
    821
    Plugins:
    15
    Minecraft User:
    Flavius12
    Machine language is one. It consists on hexadecimal instructions (called opcodes) which the CPU executes. These instructions can change between CPU architectures. Compilers which generate native executables simply convert human-readable code to machine code (often they convert the code to assembly first. Then they call the assembler program, usually GNU Assembly, which generates the machine code).
  13. aliuly
    Offline

    aliuly Notable Member Plugin Developer

    Joined:
    Feb 8, 2014
    Posts:
    1,086
    Plugins:
    17
    I would say, they are in binary rather than hex.
    EvolSoft likes this.
  14. EvolSoft
    Offline

    EvolSoft Notable Member Plugin Developer

    Joined:
    Sep 10, 2014
    Posts:
    821
    Plugins:
    15
    Minecraft User:
    Flavius12
    The result is the important at the end :p
    For convenience we represent opcodes as bytes (hexadecimal)(8 bits)(max: 255 in hex: 0xFF).
    For example if you have the assembly instruction mov al, VALUE, the Assembler will convert that instruction to 0xA0 (10100000 in binary)
    Last edited: Oct 27, 2015
  15. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder

Share This Page

Advertisement