zoukankan      html  css  js  c++  java
  • Negative numbers and binary representation

    Use -7 as sample.

    int a = -7; //declaration, notice that int is 4 bytes, so 32 bits.
    The bit representation will be in 2’s complement form.

    Steps to find 2’s Complement.
    Take binary representation of positive value (in this case, it is 7)
    0000 0000 0000 0000 0000 0000 0000 0111

    Take 1’s Complement of it.
    (Note : 1’s complement is calculated by inverting 1s with 0s and 0s with 1s.
    1111 1111 1111 1111 1111 1111 1111 1000

    Now add 1 to 1’s Complement to get 2’s complement.
    1111 1111 1111 1111 1111 1111 1111 1001

    Therefore, -7 is stored as (1111 1111 1111 1111 1111 1111 1111 1001).

    This example is only to show you that the MSB(most significant bit/ left most bit) of negative numbers is always 1. This fact will make it clear why the unsigned right shift of negative numbers results into a positive number.

    For example,
    int a = -7;
    int b = a>>>1;

  • 相关阅读:
    相关术语随笔
    JDK简介和mac下安装和查看版本命令
    英语
    英语学习2
    英语学习-19.1
    为什么java是只有值传递而没有引用传递
    线程
    关于同步异步

    jdk动态代理实现原理总结
  • 原文地址:https://www.cnblogs.com/backpacker/p/2269962.html
Copyright © 2011-2022 走看看