免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1368 | 回复: 0
打印 上一主题 下一主题

java -- 位操作符 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-06-24 15:30 |只看该作者 |倒序浏览

               
java的位操作符,最早来源于一个想法,工程师想在机顶盒里面控制硬件,而c和c++本身就有对硬件操作的位操作,所以java借鉴了这个方法。
public class C311 {
    static int a = -5;
    static int b = 5;
   
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(a >> 2);
        System.out.println(b >> 2);
        System.out.println(a  2);
        System.out.println(b  2);
        
        /** -5 用二进制表示:
         * 1)5 -- 0000 0101
         * 2)补码 -- 1111 1010
         * 3)末尾加1 -- 1111 1011 = -5
         *
         * -5 向左移2位
         * 1)末尾补0 --  1110 1100
         * 2)补码 -- 0001 0011
         * 3)末尾加1 -- 0001 0100 = -20
         *
         * -5 向右移2位
         * 1)高位补1 1111 1110
         * 2)补码 -- 0000 0001
         * 3)末尾加1 -- 0000 0010 = -2
         *
         * 5用2进制表示 -- 0000 0101
         * 5向左移动2位
         * 1)末尾补0 0001 0100 = 20
         *
         * 5向右移动2位
         * 1)高位补0 0000 0001 =1
         * */
        int c = -1;
        System.out.println(Integer.toBinaryString(c));
        System.out.println(Integer.toBinaryString(c >>> 2));
        
        /** >>> 位操作符是c、c++中没有的。
         * 这种符号规定,无论正负,都在高位补0
         *
         * */
        
        char d = 'a';
        System.out.println((d));
        System.out.println(((char)(d >> 1)));
        System.out.println(((char)(d
    }
}
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/10668/showart_1976203.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP