Articron 738 Share Posted November 27, 2017 A beginner's guide to bitwise operations Note: Before looking at this tutorial, you should at least understand what is being told in my previous tutorial on bitwise operations in scripting: https://dreambot.org/forums/index.php/topic/3911-playersettings-bitwise-operations-and-bitmasks/ Bitwise operations are operations that we use to modify binary compositions. In general, bitwise operations have three main uses in general programming. Uses of bitwise operations 1. Bit flags By using bitwise operations we can represent several variable types (primitives) into a singular variable. To give you a more practical example in relation to scripting: We could possibly store an entire GUI's settings after being filled in by the end-user in one single integer/long primitive. 2. Networking Java networking utilises a great deal of bitwise operations. If you look into the boilerplate code of the java#net package you'll find they use bitwise operations for things like integrity validation through checksums, flow control bits (bit flag), parity,.... 3. Machine arithmetics Bitwise operations are used because of a lack of arithmetic operators in languages like Assembly, to program chips/processors, machine learning,... In java, we're lucky to have the basic operators for basic mathematical equations (+, -, /, *) as the bitwise operations behind it are quite a challenge to grasp. If you're interested into learning binary arithmethics, I suggest you look at the following document: https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Comb/adder.html Bitwise operators Apart from the left & right shifts (>> and <<) we still have many other bitwise operators. In the following examples, assume that... int a = 0b0101 (=5) int b = 0b0011 (=3) 1. Bitwise AND ( & ) Example use: int c = a & b; The AND operator result can only lead to a 1-bit value when both variables have a 1-bit in the same binary index: 0b0101 0b0011 ---------- & 0b0001 (=1) 2. Bitwise OR ( | ) Example use: int c = a | b; The OR operator will result in a 1-bit value when either of the two variables' bit value is 1 in the checked binary index. If both bits are 1, this also results in a value of one. 0b0101 0b0011 ----------- | 0b0111 (=7) 3. Bitwise XOR ( ^ ) Example use: int c = a ^ b; The XOR operator will result in a 1-bit value when either of the two variables' bit value is 1 in the checked binary index. If both bits are 1, this will result in a value of zero ( = the difference with the OR operator) 0b0101 0b0011 ----------- ^ 0b0110 (=6) 4. Bitwise (unary) complement ( ~ ) Example use: int c = ~a This one is the most difficult to grasp for most. In a nutshell, the ~ operator inverts all bits. It also inverts the bit on index 32, which determines wether or not your decimal representation of this binary is positive or negative. Consider the following code: public static void main(String... args) { System.out.println(Integer.toBinaryString(Integer.MIN_VALUE)); System.out.println(Integer.toBinaryString(Integer.MAX_VALUE)); } Output: MIN_VALUE = 1000 0000 0000 0000 0000 0000 0000 0000 MAX_VALUE = 0111 1111 1111 1111 1111 1111 1111 1111 Binary works in a complementary system. This means that when we set the 32-index bit to a 1-value, the whole binary calculation is reversed. tl;dr: ~60 = -61 ~0b0100 = (1)b1011 In order to truely invert numbers using bitwise operations, we need to make use of the OR operator to apply a bitmask to get rid of the 1-value differential created because the inversion of bitIndex+1 and bitIndex+2 in relation to the original bit will always create a deficit of 1: int c = ((~a) ^ 0b1) c is equal to -5 (a = 5) Link to comment Share on other sites More sharing options...
slasso 27 Share Posted November 27, 2017 tanks for dis Link to comment Share on other sites More sharing options...
Articron 738 Author Share Posted November 27, 2017 tanks for dis I LOVE tanks dud Link to comment Share on other sites More sharing options...
bonbom1 19 Share Posted November 27, 2017 Could probably explain a little bit more what happens in the end of section 4, but other than that seems great! In order to truely invert numbers using bitwise operations, we need to make use of the OR operator to apply a bitmask to get rid of the 1-value differential created because the inversion of bitIndex+1 and bitIndex+2 in relation to the original bit will always create a deficit of 1: int c = ((~a) ^ 0b1) c is equal to -5 (a = 5) Link to comment Share on other sites More sharing options...
Articron 738 Author Share Posted November 27, 2017 Could probably explain a little bit more what happens in the end of section 4, but other than that seems great! The deficit is created by the lack of 1-value because of inversion: 0b11111111111111111111111111111111 (all bits equal to 1) has a value of -1. This is because the positive value of the 31 remaining bits is added onto Integer.MIN_VALUE. In math the Subtractor formula is as follows for two complements: -B = inverse( B ) + 1 -B - 1 = ~B Edit: Another way of looking at it is that 0 and up is included into the positive complement. -1 is the boundary for the negatives Link to comment Share on other sites More sharing options...
AI Scripting 0 Share Posted November 27, 2017 Thanks for the information. Link to comment Share on other sites More sharing options...
Notorious 342 Share Posted November 27, 2017 Honestly this is the kind of stuff I wish I had back when I started botting, before Google became my friend. But thanks for the contribution, this is written really well, and basically put into terms the whole idea of bitwise ops. Link to comment Share on other sites More sharing options...
Articron 738 Author Share Posted November 27, 2017 Honestly this is the kind of stuff I wish I had back when I started botting, before Google became my friend. But thanks for the contribution, this is written really well, and basically put into terms the whole idea of bitwise ops. IS THAT THE ONE AND ONLY NOTORIOUS Link to comment Share on other sites More sharing options...
bonbom1 19 Share Posted November 27, 2017 IS THAT THE ONE AND ONLY NOTORIOUS No it's Pete. Link to comment Share on other sites More sharing options...
Notorious 342 Share Posted November 27, 2017 IS THAT THE ONE AND ONLY NOTORIOUS Nuclear Noto Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.