classDemo2_operator { Public Static voidMain (string[] args) {/** Features of the bitwise XOR OR operator * ^ Features: one data to another data bit XOR or two times, the number itself is unchanged. */ //System.out.println (5 ^ 10 ^ 10); //System.out.println (5 ^ Ten ^ 5); /** Please implement your own exchange of two integer variables (no need to define third-party variables) * Note: In the course of lectures later, I did not explicitly specify the type of data, the default int type. */ intx = 10; inty = 5; //third-party variables are required, and development is recommended with this /*int temp; temp = x; x = y; y = temp;*/ //There is no need to define a third-party variable, there is a disadvantage, it is possible to exceed the value range of int /*x = x + y; 5 = x y = xy; 15-5 = ten x = XY; 15-10 = 5*/ //do not need third-party variables, through ^ to dox = x ^ y;//Ten ^ 5y = x ^ y;//Ten ^ 5 ^ 5 y = Tenx = x ^ y;//Ten ^ 5 ^ ten x = 5System.out.println ("x =" + x + ", y =" +y); }}
The most efficient multiplication operation
classDemo3_operator { Public Static voidMain (string[] args) {/** <<: Left to left the highest bit discarded, 0 * >> on the right;: Shift right to the highest bit is 0, left to 0, up to 1, left to 1 * >>>: Unsigned Right shift Whether the highest bit is 0 or 1, the left side is 0 * The result of 2 * 8 is calculated most efficiently*/ //shift left, move left a few is multiplied by 2 power several times//System.out.println (<< 1); // -//System.out.println (<< 2); // - /*00000000 00000000 00000000 00001100 12 complement (0) 0000000 00000000 00000000 000011000 24 complement ( 00) 000000 00000000 00000000 0000110000 48 complement*/ //shift right, move to the right, and divide by 2 power several times.//System.out.println (>> 1); //System.out.println (>> 2); /*00000000 00000000 00000000 00001100 12 complement 000000000 00000000 00000000 0000110 (0) 6 000 0000000 00000000 00000000 000011 (XX) 3*/ //The results of 2 * 8 are calculated most efficientlySYSTEM.OUT.PRINTLN (2 << 3); }}
Java Interview bit operations (how to Exchange two numbers without a third-party variable, the most efficient multiplication operation)