how to use these operators? from pocketmine: PHP: $this->sneaking = (($flags & 0x40) > 0); PHP: $flags |= 0x100; PHP: return PHP_INT_SIZE === 8 ? (($x & 0xFFFFFFF) << 35) | (($y & 0x7f) << 28) | ($z & 0xFFFFFFF) : $x . ":" . $y .":". $z;//mean "|" operator
Before creating a thread, why not look at the PHP manual? PS: They are called bitwise operators, learn about them here: http://php.net/manual/en/language.operators.bitwise.php
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 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]
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
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.
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).
The result is the important at the end 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)