Java 反转位数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3165776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:02:01  来源:igfitidea点击:

Reverse bits in number

java

提问by dato datuashvili

For example, I have the binary number 1011 which is equal to decimal 11. I want the reverse bit's location such that it become 1101, which is decimal 13. Here is code:

例如,我有一个二进制数 1011,它等于十进制 11。我想要反向位的位置,使其变为 1101,即十进制 13。这是代码:

import java.util.*;
public class bits {
    public static void main(String[] args) {
        Scanner scnr=new Scanner(System.in);
        System.out.println("enter x:");
        int x=scnr.nextInt();
        int b=0;
        while (x!=0){
            b|=( x &1);
            x>>=1;
            b<<=1;
        }
        System.out.println(b);
    }
}

But when I enter x 11 then it prints 26. What is the mistake?

但是当我输入 x 11 时,它会打印 26。错误是什么?

采纳答案by Thomas

You are shifting bone time too many. Do the shift first (so that the first time, when b == 0, it has no effect):

你移动的次数b太多了。先做班次(这样第一次,当b == 0,它没有效果):

while (x!=0){
  b<<=1;
  b|=( x &1);
  x>>=1;
}

回答by Peter G.

b is shifted left once too often. I expect input 1 to result in output 2. Move the Shift two lines up.

b 经常左移一次。我希望输入 1 产生输出 2。将 Shift 向上移动两行。

回答by maranas

you shifted b once too many. try shifting b to the left before doing the |=:

你移动 b 一次太多了。在执行 |= 之前尝试将 b 向左移动:

    while (x!=0){
           b<<=1;
           b|=( x &1);
           x>>=1;

         }
   System.out.println(b);

回答by Tansir1

Slightly offtopic. There's also the option of the built-in bit reversing features of Java.

有点题外话。还可以选择 Java 的内置位反转功能。

See http://java.sun.com/javase/6/docs/api/java/lang/Integer.html#reverse(int)

http://java.sun.com/javase/6/docs/api/java/lang/Integer.html#reverse(int)

EDIT: This assumes you're using Java 1.5 or newer.

编辑:这假设您使用的是 Java 1.5 或更新版本。

回答by Praetorian

You're left shifting bone time more than required. Add b >>= 1after your while loop.

b比所需的移动次数多了一倍。b >>= 1在 while 循环之后添加。

回答by Andreas Dolk

The result is twice as much as expected so the last left shift operation (one left shift doubles the value) is too much.

结果是预期的两倍,所以最后一次左移操作(一次左移将值加倍)太多了。

回答by user2053554

  1. Use >>>=instead of >>=
  2. If you want to change method signature to public static byte reverse(byte in)this will not work on negative values because there is implicit cast to int.
  1. 使用>>>=代替>>=
  2. 如果您想将方法签名更改为public static byte reverse(byte in)this 将不适用于负值,因为隐式转换为 int。

回答by Murali Mohan

It is safe to use the unsigned right shift operator (>>>) in the while loop to obviate the danger of running into an infinite loop for -ve numbers.

在 while 循环中使用无符号右移运算符 (>>>) 来避免 -ve 数字陷入无限循环的危险是安全的。

while (x!=0){
  b<<=1;
  b|=( x &1);
  x>>>=1;
}

回答by Lester

The program do not work for input like 1, 2

该程序不适用于像 1, 2 这样的输入

int reverseBits(int x)
    {
        int b = 0;
        while (x != 0)
        {
            b <<= 1;
            b |= ( x & 1);
            x >>= 1
        }
        return b;
    }

input 1 output 1, should be 8 right? input 2 output 1, should be 4.

输入1输出1,应该是8吧?输入2输出1,应该是4。

回答by Rahul

while(x!=0){
    b<<=1;
    b|=(x&1);
    x>>=1;
}