Java 倒数位

Java 倒数位,java,Java,例如,我有一个二进制数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.nex

例如,我有一个二进制数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);
    }
}
import java.util.*;
公共类比特{
公共静态void main(字符串[]args){
扫描仪scnr=新扫描仪(System.in);
System.out.println(“输入x:”);
int x=scnr.nextInt();
int b=0;
而(x!=0){
b |=(x&1);
x> >=1;

b您移动了
b
一次太多。请先进行移动(这样,当
b==0
时,第一次移动没有效果):

while(x!=0){
b=1;
}

b左移太频繁了。我希望输入1会导致输出2。将移位向上移动两行。

您将b移了太多。在执行|=之前,请尝试将b向左移动:

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

         }
   System.out.println(b);
while(x!=0){
b=1;
}
系统输出打印ln(b);

有点离题。还有Java内置的位反转功能选项


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

您左移的
b
比要求的多一倍。在while循环后添加
b>=1

结果是预期的两倍,因此上次左移操作(一次左移使值加倍)太多

  • 使用
    >>>=
    而不是
    >>=
  • 如果您想将方法签名更改为
    公共静态字节反转(byte-in)
    这对负值不起作用,因为存在对int的隐式转换

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

    while (x!=0){
      b<<=1;
      b|=( x &1);
      x>>>=1;
    }
    
    while(x!=0){
    b> =1;
    }
    
    该程序不适用于1、2等输入

    int reverseBits(int x)
        {
            int b = 0;
            while (x != 0)
            {
                b <<= 1;
                b |= ( x & 1);
                x >>= 1
            }
            return b;
        }
    
    int反向息税前利润(int x)
    {
    int b=0;
    而(x!=0)
    {
    b=1
    }
    返回b;
    }
    
    输入1输出1,应该是8对吗? 输入2输出1,应为4。

    while(x!=0){
    
    while(x!=0){
        b<<=1;
        b|=(x&1);
        x>>=1;
    }
    
    b=1; }
    初学者注意:我使用十六进制(0-9和A-F),因为一个十六进制数字可以完美地映射为4个二进制位。我使用A(10十进制)代替1010。您可以告诉Java使用十六进制(文字),从0x开始,就像在0x0A中一样

    如前所述,1应输出8(0001到1000)。 因此,与while(x!=0)不同,代码需要将第一位移位,直到本例中所需的位长度为4为止

    for (int i = 0; i < 4; ++i) { // not while (x!=0){
       b<<=1;
       b|=( x &1);
       x>>=1;
    }
    Hex convert 0-F: 0=0 1=8 2=4 3=C 4=2 5=A 6=6 7=E 8=1 9=9 A=5 B=D C=3 D=B E=7 F=F
    

    我的新java代码使用具有强大位操作的java将位反转为整数。它可以处理正值、负值和零值。希望对您有所帮助

    public static int  reverseDigits(int num) throws Exception {
            if (num == 0) {         
                return Integer.MAX_VALUE | Integer.MIN_VALUE;
            }
    
            int count = Integer.SIZE * 8 - 1;
            int  reversed = num;        
            boolean positive = true;
    
            if (num < 0) {
                positive = false;
            }
    
            if (positive) num >>= 1;
    
            while(num != 0) {
    
                reversed <<= 1; 
                reversed |= (num & 1);          
    
                num >>>= 1;
                count--;            
            }
    
            if (positive) reversed <<= count;
            return reversed;
        }
    
    public static int reverseDigits(int num)引发异常{
    如果(num==0){
    返回Integer.MAX_值| Integer.MIN_值;
    }
    int count=Integer.SIZE*8-1;
    int=num;
    布尔正=真;
    if(num<0){
    阳性=假;
    }
    如果(正)num>>=1;
    while(num!=0){
    反向>=1;
    计数--;
    }
    
    如果(正)反转一个完整的整数可以用
    整数反转。反转(int i)
    -但是,因为看起来您想要用更少的位反转整数,所以我将其作为注释。看起来解决方案已经在这里了:您缺少需要在结尾处进行额外移位的功能。如果使用这一行,效果更好:布尔正=(num<0)?false:true;忘记下一个if语句。“throws Exception”并不重要,我只在其他测试中使用它。或者
    boolean positive=num>=0;
    public static byte reverse(byte x) {
        byte b = 0;
        for (int i = 0; i < 8; ++i) {
            b<<=1;
            b|=( x &1);
            x>>=1;
          }
        return b;
    }
    public static void main(String args[]) {
        byte[] nums = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 
                (byte) 0xAA, (byte) 0xFE, (byte) 0xFF };
        for (byte b : nums) {
            System.out.printf("%02X=%02X ", b, reverse(b));
        }
        System.out.println();
    }
    
    00=00 01=80 02=40 03=C0 04=20 05=A0 06=60 07=E0 08=10
    09=90 0A=50 0B=D0 0C=30 0D=B0 0E=70 0F=F0 10=08 11=88 AA=55 FE=7F FF=FF
    
    public static int  reverseDigits(int num) throws Exception {
            if (num == 0) {         
                return Integer.MAX_VALUE | Integer.MIN_VALUE;
            }
    
            int count = Integer.SIZE * 8 - 1;
            int  reversed = num;        
            boolean positive = true;
    
            if (num < 0) {
                positive = false;
            }
    
            if (positive) num >>= 1;
    
            while(num != 0) {
    
                reversed <<= 1; 
                reversed |= (num & 1);          
    
                num >>>= 1;
                count--;            
            }
    
            if (positive) reversed <<= count;
            return reversed;
        }