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

桃扇骨 2022-06-10 01:09 241阅读 0赞

实现有符号整数的二进制表示法。或者说,实现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才能避免这个问题。

代码实现如下:

  1. /**
  2. * Implement java.lang.Integer.toBinaryString.
  3. */
  4. public class ConvertBinary {
  5. public ConvertBinary() {
  6. super();
  7. }
  8. /**
  9. * @param num The number to be converted to binary string
  10. * @return String representation of the 2's complement of the number.
  11. */
  12. public String toBinaryString(int num) {
  13. Long new_num = Long.valueOf(num); // Case to Long to deal with Integer.MIN_VALUE
  14. if (num == 0) {
  15. return "0";
  16. } else if (num > 0) {
  17. // the 2's complement of a positive number is the binary representation
  18. // of the number
  19. return toBaseTwo(new_num, false);
  20. } else { // num < 0
  21. // the 2's complement of a negative number is the 1's complement of that number plus 1.
  22. // the 1's complement of a negative number can be obtained by reverting all the digits
  23. // of the base-two representation of it's absolute value.
  24. new_num *= -1;
  25. String result = toBaseTwo(new_num, true);
  26. result = revertDigit(result);
  27. result = addOne(result);
  28. return result;
  29. }
  30. }
  31. /**
  32. * @param num The number to be converted to base 2 representation
  33. * @param flag Boolean flag to indicates if 0s need to be complemented
  34. * to make the base 2 representation 32 digits long. This is needed for negative original inputs.
  35. * @return String representation of a base 2 representation.
  36. */
  37. private String toBaseTwo(Long num, boolean flag) {
  38. StringBuilder sb = new StringBuilder();
  39. while (num > 0) {
  40. long curr = num % 2;
  41. num = num / 2;
  42. sb.append(curr);
  43. }
  44. if (flag) {
  45. while (sb.length() < 32) {
  46. // add extra 0s to the binary string to make it 32 bits long
  47. sb.append('0');
  48. }
  49. }
  50. return sb.reverse().toString();
  51. }
  52. private String revertDigit(String num) {
  53. StringBuilder sb = new StringBuilder();
  54. for (int i = 0; i < num.length(); i++) {
  55. sb.append(num.charAt(i) == '0' ? '1' : '0');
  56. }
  57. return sb.toString();
  58. }
  59. private String addOne(String num) {
  60. StringBuilder sb = new StringBuilder();
  61. int carryOver = 1;
  62. int i = num.length() - 1;
  63. for (; i >= 0; i--) {
  64. int curr = num.charAt(i) - '0';
  65. curr += carryOver;
  66. if (curr == 2) {
  67. carryOver = 1;
  68. curr = 0;
  69. } else {
  70. carryOver = 0;
  71. }
  72. sb.append(curr);
  73. }
  74. return sb.reverse().toString();
  75. }
  76. }

发表评论

表情:
评论列表 (有 0 条评论,241人围观)

还没有评论,来说两句吧...

相关阅读