Java Shift Operators

Source: Internet
Author: User

Importorg.junit.Test;/*** 1) <<: Left shift operator * 2) >>: Right shift operator (test positive) * 3) >>: Right shift operator (test negative) * 4) >>>: Unsigned Right shift (test positive) * 5) > >>: Unsigned Right shift (test negative)*/ Public classWeiyitest {/*** <<: Left shift operator * Test number: 101*/@Test Public voidtest1 () {System.out.println (integer.tobinarystring (101));//1100101System.out.println (101 << 8);//25856System.out.println (integer.tobinarystring (101 << 8));//110010100000000        /** Left 8-bit logic * 01100101//Original Data 101 * 01100101//Left shift 8 bits, right free position with 0 complement * <----<---<----<-* 01100101 00000000//Displacement data, 25856*/System.out.println (Integer.parseint ("0110010100000000", 2));//25856// ==================================================================== //System.out.println (101 << 16);//6619136System.out.println (integer.tobinarystring (101 << 16));//1100101 00000000 00000000        /** Shift left 16-bit logic * 01100101//Original Data 101 * 01100101//Left Shift 16-bit, right free position with 0 full * <----<---<----<----<----< * 01100101 0000000 00000000//Offset Data, 6619136*/System.out.println (Integer.parseint ("011001010000000000000000", 2));//6619136    }    /*** >>: Right shift operator *----------------* Test positive: 1010001001*/@Test Public voidtest2_1 () {System.out.println (integer.tobinarystring (1010001001));//111100001100110110010001101001System.out.println (1010001001 >> 8);//3945316System.out.println (integer.tobinarystring (1010001001 >> 8));//1111000011001101100100        /** Right Shift 8-bit logic * 00111100 00110011 01100100 01101001 * 00111100 00110011 01100100/ /Right displacement 8 bit, left free position with 0 complete *---->--->---->----->---->---->----> * 00000000 00111100 00 110011 01100100//displacement of data obtained, 3945316*/System.out.println (Integer.parseint ("001111000011001101100100", 2));//3945316// ==================================================================== //System.out.println (1010001001 >> 16);//15411System.out.println (integer.tobinarystring (1010001001 >> 16));//11110000110011        /** Move right 16-bit logic * 00111100 00110011 01100100 01101001 * 00111100 00110011 Shift right 16 bits, left free position with 0 fill *---->--->---->----->---->---->----> * 00000000 00000000 00111100 00110011//displacement of data obtained, 15411*/System.out.println (Integer.parseint ("0011110000110011", 2));//15411    }    /*** >>: Right shift operator * Test negative: -1010001001 *--------------------------------* After displacement, or negative, sign bit not changed*/@Test Public voidtest2_2 () {System.out.println (integer.tobinarystring (-1010001001));//11000011110011001001101110010111System.out.println (-1010001001 >> 8);//-3945317System.out.println (integer.tobinarystring ( -1010001001 >> 8));//11111111110000111100110010011011        /** Right Shift 8-bit logic * 11000011 11001100 10011011 10010111 * 11000011 11001100 10011011/ /Right displacement 8 bit, left free position with 1 complete *---->--->---->----->---->---->----> * 11111111 11000011 11 001100 10011011//Displacement data obtained, -3945317*/        // ==================================================================== //System.out.println (-1010001001 >> 16);//-15412System.out.println (integer.tobinarystring ( -1010001001 >> 16));//11111111111111111100001111001100        /** Move right 16-bit logic * 11000011 11001100 10011011 10010111 * 11000011 11001100 Shift right 16 bits, left free position with 1 fill *---->--->---->----->---->---->----> * 11111111 11111111 11000011 11001100//Displacement data obtained, -15412*/    }    /*** >>>: Unsigned Right shift * Test positive: 1010001001*/@Test Public voidTest3_1 () {System.out.println (integer.tobinarystring (1010001001));//111100001100110110010001101001System.out.println (1010001001 >>> 8);//3945316System.out.println (integer.tobinarystring (1010001001 >>> 8));//1111000011001101100100        /** Right Shift 8-bit logic * 00111100 00110011 01100100 01101001 * 00111100 00110011 01100100/ /Right displacement 8 bit, left free position with 0 complete *---->--->---->----->---->---->----> * 00000000 00111100 00 110011 01100100//displacement of data obtained, 3945316*/System.out.println (Integer.parseint ("001111000011001101100100", 2));//3945316// ==================================================================== //System.out.println (1010001001 >>> 16);//15411System.out.println (integer.tobinarystring (1010001001 >>> 16));//11110000110011        /** Move right 16-bit logic * 00111100 00110011 01100100 01101001 * 00111100 00110011 Shift right 16 bits, left free position with 0 fill *---->--->---->----->---->---->----> * 00000000 00000000 00111100 00110011//displacement of data obtained, 15411*/System.out.println (Integer.parseint ("0011110000110011", 2));//15411    }    /*** >>>: Unsigned Right Shift * Test negative: -1010001001 *-----------------------------* Displacement, negative number becomes positive*/@Test Public voidTest3_2 () {System.out.println (integer.tobinarystring (-1010001001));//11000011110011001001101110010111System.out.println (-1010001001 >>> 8);//12831899System.out.println (integer.tobinarystring ( -1010001001 >>> 8));//110000111100110010011011        /** Right Shift 8-bit logic * 11000011 11001100 10011011 10010111 * 11000011 11001100 10011011/ /Right displacement 8 bit, left free position with 0 complete *---->--->---->----->---->---->----> * 00000000 11000011 11 001100 10011011//displacement of data obtained, 12831899*/System.out.println (Integer.parseint ("110000111100110010011011", 2));//12831899// ==================================================================== //System.out.println (-1010001001 >>> 16);//50124System.out.println (integer.tobinarystring ( -1010001001 >>> 16));//1100001111001100        /** Move right 16-bit logic * 11000011 11001100 10011011 10010111 * 11000011 11001100 Shift right 16 bits, left free position with 0 fill *---->--->---->----->---->---->----> * 00000000 00000000 11000011 11001100//displacement of data obtained, 50124*/System.out.println (Integer.parseint ("1100001111001100", 2));//50124    }}

Java Shift Operators

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.