Amazon面试题 实现有符号整数的二进制表示法

Java有符号整数二进制转换
本文介绍如何实现Java中有符号整数到二进制字符串的转换,重点讲解了补码的概念及其在实现过程中的应用,并提供了一个具体的代码实现。

实现有符号整数的二进制表示法。或者说,实现java.lang.Integer.toBinaryString()方法。

要想实现有符号整数的二进制表示法,我们首先需要知道有符号整数在计算机中是怎么存储的。

计算机中存储有符号整数,使用的是补码(two’s complement)。正数的补码同原码(其二进制表示)相同。负数的补码是其绝对值的原码按位取反(反码,one’s complement)后再加一。因此,在补码中只有一个0,也就是00000000000000000000000000000000。而10000000000000000000000000000000是最小的负数,在Java中也就是Integer.MIN_VALUE

同时,这也带来一个我们在实现toBinaryString()函数时需要注意的问题,因为Java中Integer.MAX_VALUE,也就是01111111111111111111111111111111,的绝对值比Integer.MIN_VALUE小1。所以如果我们先求Integer.MIN_VALUE绝对值再求其二进制原码表示的话就会产生溢出。因此需要先将输入转化为Long才能避免这个问题。

代码实现如下:

/**
 * Implement java.lang.Integer.toBinaryString.
 */
public class ConvertBinary {
    public ConvertBinary() {
        super();
    }

    /**
     * @param num The number to be converted to binary string
     * @return String representation of the 2's complement of the number.
     */
    public String toBinaryString(int num) {
        Long new_num = Long.valueOf(num); // Case to Long to deal with Integer.MIN_VALUE
        if (num == 0) {
            return "0";
        } else if (num > 0) {
            // the 2's complement of a positive number is the binary representation
            // of the number

            return toBaseTwo(new_num, false);
        } else { // num < 0
            // the 2's complement of a negative number is the 1's complement of that number plus 1.
            // the 1's complement of a negative number can be obtained by reverting all the digits
            // of the base-two representation of it's absolute value.
            new_num *= -1;
            String result = toBaseTwo(new_num, true);
            result = revertDigit(result);
            result = addOne(result);
            return result;
        }
    }

    /**
     * @param num The number to be converted to base 2 representation
     * @param flag Boolean flag to indicates if 0s need to be complemented
     *      to make the base 2 representation 32 digits long. This is needed for negative original inputs.
     * @return String representation of a base 2 representation.
     */
    private String toBaseTwo(Long num, boolean flag) {
        StringBuilder sb = new StringBuilder();
        while (num > 0) {
            long curr = num % 2;
            num = num / 2;
            sb.append(curr);
        }
        if (flag) {
            while (sb.length() < 32) {
                // add extra 0s to the binary string to make it 32 bits long
                sb.append('0');
            }
        }

        return sb.reverse().toString();
    }

    private String revertDigit(String num) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < num.length(); i++) {
            sb.append(num.charAt(i) == '0' ? '1' : '0');
        }

        return sb.toString();
    }

    private String addOne(String num) {
        StringBuilder sb = new StringBuilder();
        int carryOver = 1;
        int i = num.length() - 1;
        for (; i >= 0; i--) {
            int curr = num.charAt(i) - '0';
            curr += carryOver;
            if (curr == 2) {
                carryOver = 1;
                curr = 0;
            } else {
                carryOver = 0;
            }
            sb.append(curr);
        }

        return sb.reverse().toString();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

耀凯考前突击大师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值