java 位移運算子

來源:互聯網
上載者:User

標籤:符號   void   public   imp   正數   測試   binary   unit   col   

import org.junit.Test;/** * 1)<< : 左移運算子 * 2)>> : 右移運算子 (測試正數) * 3)>> : 右移運算子 (測試負數) * 4)>>> : 無符號右移 (測試正數) * 5)>>> : 無符號右移 (測試負數) */public class WeiYiTest {    /**     * << : 左移運算子     * 測試數字:101     */    @Test    public void test1() {        System.out.println(Integer.toBinaryString(101)); // 1100101        System.out.println(101 << 8); // 25856        System.out.println(Integer.toBinaryString(101 << 8)); // 110010100000000        /*         * 左移8位邏輯         *            01100101    // 原資料101         *   01100101             // 向左位移8位,右側空餘的位置用0補全         *   <----<---<----<-         *   01100101 00000000    // 位移後得到的資料,25856         */        System.out.println(Integer.parseInt("0110010100000000", 2)); // 25856        // ==================================================================== //        System.out.println(101 << 16); // 6619136        System.out.println(Integer.toBinaryString(101 << 16)); // 1100101 00000000 00000000        /*         * 左移16位邏輯         *                    01100101    // 原資料101         *   01100101                     // 向左位移16位,右側空餘的位置用0補全         *   <----<---<----<----<----<         *   01100101 0000000 00000000    // 位移後得到的資料,6619136         */        System.out.println(Integer.parseInt("011001010000000000000000", 2)); // 6619136    }    /**     * >> : 右移運算子     * ----------------     * 測試正數:1010001001     */    @Test    public void test2_1() {        System.out.println(Integer.toBinaryString(1010001001)); // 111100001100110110010001101001        System.out.println(1010001001 >> 8); // 3945316        System.out.println(Integer.toBinaryString(1010001001 >> 8)); // 1111000011001101100100        /*         * 右移8位邏輯         *   00111100 00110011 01100100 01101001         *            00111100 00110011 01100100    // 向右位移8位,左側空餘的位置用0補全         *   ---->--->---->----->---->---->---->         *   00000000 00111100 00110011 01100100    // 位移後得到的資料,3945316         */        System.out.println(Integer.parseInt("001111000011001101100100", 2)); // 3945316        // ==================================================================== //        System.out.println(1010001001 >> 16); // 15411        System.out.println(Integer.toBinaryString(1010001001 >> 16)); // 11110000110011        /*         * 右移16位邏輯         *   00111100 00110011 01100100 01101001         *                     00111100 00110011    // 向右位移16位,左側空餘的位置用0補全         *   ---->--->---->----->---->---->---->         *   00000000 00000000 00111100 00110011    // 位移後得到的資料,15411         */        System.out.println(Integer.parseInt("0011110000110011", 2)); // 15411    }    /**     * >> : 右移運算子     * 測試負數:-1010001001     * --------------------------------     * 位移後,還是負數,符號位沒有改變     */    @Test    public void test2_2() {        System.out.println(Integer.toBinaryString(-1010001001)); // 11000011110011001001101110010111        System.out.println(-1010001001 >> 8); // -3945317        System.out.println(Integer.toBinaryString(-1010001001 >> 8)); // 11111111110000111100110010011011        /*         * 右移8位邏輯         *   11000011 11001100 10011011 10010111         *            11000011 11001100 10011011    // 向右位移8位,左側空餘的位置用1補全         *   ---->--->---->----->---->---->---->         *   11111111 11000011 11001100 10011011    // 位移後得到的資料,-3945317         */        // ==================================================================== //        System.out.println(-1010001001 >> 16); // -15412        System.out.println(Integer.toBinaryString(-1010001001 >> 16)); // 11111111111111111100001111001100        /*         * 右移16位邏輯         *   11000011 11001100 10011011 10010111         *                     11000011 11001100    // 向右位移16位,左側空餘的位置用1補全         *   ---->--->---->----->---->---->---->         *   11111111 11111111 11000011 11001100    // 位移後得到的資料,-15412         */    }    /**     * >>> : 無符號右移     * 測試正數:1010001001     */    @Test    public void test3_1() {        System.out.println(Integer.toBinaryString(1010001001)); // 111100001100110110010001101001        System.out.println(1010001001 >>> 8); // 3945316        System.out.println(Integer.toBinaryString(1010001001 >>> 8)); // 1111000011001101100100        /*         * 右移8位邏輯         *   00111100 00110011 01100100 01101001         *            00111100 00110011 01100100    // 向右位移8位,左側空餘的位置用0補全         *   ---->--->---->----->---->---->---->         *   00000000 00111100 00110011 01100100    // 位移後得到的資料,3945316         */        System.out.println(Integer.parseInt("001111000011001101100100", 2)); // 3945316        // ==================================================================== //        System.out.println(1010001001 >>> 16); // 15411        System.out.println(Integer.toBinaryString(1010001001 >>> 16)); // 11110000110011        /*         * 右移16位邏輯         *   00111100 00110011 01100100 01101001         *                     00111100 00110011    // 向右位移16位,左側空餘的位置用0補全         *   ---->--->---->----->---->---->---->         *   00000000 00000000 00111100 00110011    // 位移後得到的資料,15411         */        System.out.println(Integer.parseInt("0011110000110011", 2)); // 15411    }    /**     * >>> : 無符號右移     * 測試負數:-1010001001     * -----------------------------     * 位移後,負數變正數了     */    @Test    public void test3_2() {        System.out.println(Integer.toBinaryString(-1010001001)); // 11000011110011001001101110010111        System.out.println(-1010001001 >>> 8); // 12831899        System.out.println(Integer.toBinaryString(-1010001001 >>> 8)); // 110000111100110010011011        /*         * 右移8位邏輯         *   11000011 11001100 10011011 10010111         *            11000011 11001100 10011011    // 向右位移8位,左側空餘的位置用0補全         *   ---->--->---->----->---->---->---->         *   00000000 11000011 11001100 10011011    // 位移後得到的資料,12831899         */        System.out.println(Integer.parseInt("110000111100110010011011", 2)); // 12831899        // ==================================================================== //        System.out.println(-1010001001 >>> 16); // 50124        System.out.println(Integer.toBinaryString(-1010001001 >>> 16)); // 1100001111001100        /*         * 右移16位邏輯         *   11000011 11001100 10011011 10010111         *                     11000011 11001100    // 向右位移16位,左側空餘的位置用0補全         *   ---->--->---->----->---->---->---->         *   00000000 00000000 11000011 11001100    // 位移後得到的資料,50124         */        System.out.println(Integer.parseInt("1100001111001100", 2)); // 50124    }}

 

java 位移運算子

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.