Java Interview bit operations (how to Exchange two numbers without a third-party variable, the most efficient multiplication operation)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.