Java位操作行为奇怪

Java位操作行为奇怪,java,image-processing,byte,bit,Java,Image Processing,Byte,Bit,我目前有简单的功能 public static void convert() { for (int i = 0; i < byteArray.length; i++) { byteArray[i] = (byte) (byteArray[i] & ~(1 << 0)); String s = ("0000000" + Integer.toBinaryString(0xFF & byteArray[i])).repl

我目前有简单的功能

public static void convert() {
     for (int i = 0; i < byteArray.length; i++) {
         byteArray[i] = (byte) (byteArray[i] & ~(1 << 0));

         String s = ("0000000" + Integer.toBinaryString(0xFF & byteArray[i])).replaceAll(".*(.{8})$", "$1");
         System.out.println(s);
     }
}
奇怪的是,当我在函数中的行周围添加一个简单的if语句(它不会改变任何东西)时,输出会完全改变。例如,如果我将上述函数更改为

public static void convert() {
     for (int i = 0; i < byteArray.length; i++) {

        if (5 > 3) {
         byteArray[i] = (byte) (byteArray[i] & ~(1 << 0));

         String s = ("0000000" + Integer.toBinaryString(0xFF & byteArray[i])).replaceAll(".*(.{8})$", "$1");
         System.out.println(s);
        }
     }
}
我真的很困惑为什么会发生这种情况。谢谢

全班同学仅供参考:

public class LIB {
static byte[] byteArray;
static ArrayList<String> bitArray = new ArrayList<String>();
static String messageToDecode = "001010110";
static char[] mtdChar = messageToDecode.toCharArray();
static ArrayList<String> FinalBitArray = new ArrayList<String>();

public static void main(String[] args) throws IOException  {
    // TODO Auto-generated method stub
    imageToBits();
    convert();

}

public static void imageToBits () throws IOException {
    //get image bytes
    byteArray = Files.readAllBytes(new File("/Users/2020shatgiskessell/Desktop/url.jpg").toPath());
     for (int i = 0; i < byteArray.length; i++) {

         String s = ("0000000" + Integer.toBinaryString(0xFF & byteArray[i])).replaceAll(".*(.{8})$", "$1");
         bitArray.add(s);

     }
}


public static void convert() {
     for (int i = 0; i < byteArray.length; i++) {

        if (5 > 3) {
         byteArray[i] = (byte) (byteArray[i] & ~(1 << 0));

         String s = ("0000000" + Integer.toBinaryString(0xFF & byteArray[i])).replaceAll(".*(.{8})$", "$1");
         System.out.println(s);
        }
     }
}
公共类库{
静态字节[]字节数组;
静态ArrayList bitArray=新ArrayList();
静态字符串messageToDecode=“001010110”;
静态字符[]mtdChar=messageToDecode.tocharray();
静态ArrayList FinalBitArray=新ArrayList();
公共静态void main(字符串[]args)引发IOException{
//TODO自动生成的方法存根
imageToBits();
convert();
}
公共静态void imageToBits()引发IOException{
//获取图像字节
byteArray=Files.readAllBytes(新文件(“/Users/2020shatgiskessell/Desktop/url.jpg”).toPath();
for(int i=0;i3){

我开始怀疑@LittleSanti提到的愚人节笑话。如果是这样,那就意味着

我写了以下内容;我消除了源数组中的赋值,并将“奇怪的行为”条件放在循环中,而不是将其分为两次运行,这样我们就可以更容易地看到结果。在这里的表达式(当然是奇怪的)中没有这种奇怪的行为

package bitplay;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;

public class Bitplay
{
//    public class LIB {
    static byte[] byteArray;
    static ArrayList<String> bitArray = new ArrayList<String>();
    static String messageToDecode = "001010110";
    static char[] mtdChar = messageToDecode.toCharArray();
    static ArrayList<String> FinalBitArray = new ArrayList<String>();

    public static void main(String[] args) throws IOException  {
      fakeRoutine();
    }

    private static void fakeRoutine()
    {
      byte[] array = new byte[] { (byte)0b11110111, 
                                  (byte)0b11000011,
                                  (byte)0b11111010 
                                };
      for (int i=0; i<3; i++)
      {
        String s = ("0000000" + Integer.toBinaryString(0xFF & array[i])).replaceAll(".*(.{8})$", "$1");
        System.out.println("1: " + s);

        byte clearedOne = (byte) (array[i] & ~(1 << 0));
        s = ("0000000" + Integer.toBinaryString(0xFF & clearedOne)).replaceAll(".*(.{8})$", "$1");
        System.out.println("2: " + s);

        if (5 > 3)
        {
          clearedOne = (byte) (array[i] & ~(1 << 0));
          s = ("0000000" + Integer.toBinaryString(0xFF & clearedOne)).replaceAll(".*(.{8})$", "$1");
          System.out.println("3: " + s);
        }
      }

    }
}

正如所料。如果你提出了一个更具体的问题,尽管问吧,但我已经回答完这个问题。

我开始怀疑@LittleSanti提到的愚人节笑话。如果是这样,那就太卑鄙了

我写了以下内容;我消除了源数组中的赋值,并将“奇怪的行为”条件放在循环中,而不是将其分为两次运行,这样我们就可以更容易地看到结果。在这里的表达式(当然是奇怪的)中没有这种奇怪的行为

package bitplay;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;

public class Bitplay
{
//    public class LIB {
    static byte[] byteArray;
    static ArrayList<String> bitArray = new ArrayList<String>();
    static String messageToDecode = "001010110";
    static char[] mtdChar = messageToDecode.toCharArray();
    static ArrayList<String> FinalBitArray = new ArrayList<String>();

    public static void main(String[] args) throws IOException  {
      fakeRoutine();
    }

    private static void fakeRoutine()
    {
      byte[] array = new byte[] { (byte)0b11110111, 
                                  (byte)0b11000011,
                                  (byte)0b11111010 
                                };
      for (int i=0; i<3; i++)
      {
        String s = ("0000000" + Integer.toBinaryString(0xFF & array[i])).replaceAll(".*(.{8})$", "$1");
        System.out.println("1: " + s);

        byte clearedOne = (byte) (array[i] & ~(1 << 0));
        s = ("0000000" + Integer.toBinaryString(0xFF & clearedOne)).replaceAll(".*(.{8})$", "$1");
        System.out.println("2: " + s);

        if (5 > 3)
        {
          clearedOne = (byte) (array[i] & ~(1 << 0));
          s = ("0000000" + Integer.toBinaryString(0xFF & clearedOne)).replaceAll(".*(.{8})$", "$1");
          System.out.println("3: " + s);
        }
      }

    }
}

正如预期的那样。如果你提出了一个更具体的问题,请继续提问,但我已经回答完这个问题。

你要求我们为你调试代码,但你甚至没有提供所有代码。将其放入调试器,找出哪个语句没有达到你的预期。然后删除此帖子,除非你需要帮助该语句。@arcy我提供了除main函数之外的所有代码,main函数只是调用这些函数。我不确定为什么会发生此错误,这也是我接触堆栈溢出社区的原因。调试器没有提供有关为什么会发生此错误的信息。
my_string.substring(0,my_string.length()-2)+“1”
是完成任务的一种方法,可能不是最好的。@fallacoulibly当我这样做时,问题仍然存在that@StephaneHatgis-Kessell如果您的问题是将字符串中最右边的字符替换为1,则我提供的代码可以工作。您要求我们为您调试代码,但您甚至没有提供所有代码。请将其放入调试器并找出哪些语句没有达到预期效果。然后删除此帖子,除非您需要帮助理解该语句。@arcy我提供了除main函数之外的所有代码,main函数只是调用这些函数。我不确定为什么会发生此错误,这也是我接触堆栈溢出社区的原因。debugger没有给出发生这种情况的原因。
my_string.substring(0,my_string.length()-2)+“1”
是完成任务的一种方法,可能不是最好的。@fallacoulibly当我这样做时,问题仍然存在that@StephaneHatgis-Kessell如果您的问题是将字符串中最右边的字符替换为1,那么我提供的代码可以工作。
package bitplay;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;

public class Bitplay
{
//    public class LIB {
    static byte[] byteArray;
    static ArrayList<String> bitArray = new ArrayList<String>();
    static String messageToDecode = "001010110";
    static char[] mtdChar = messageToDecode.toCharArray();
    static ArrayList<String> FinalBitArray = new ArrayList<String>();

    public static void main(String[] args) throws IOException  {
      fakeRoutine();
    }

    private static void fakeRoutine()
    {
      byte[] array = new byte[] { (byte)0b11110111, 
                                  (byte)0b11000011,
                                  (byte)0b11111010 
                                };
      for (int i=0; i<3; i++)
      {
        String s = ("0000000" + Integer.toBinaryString(0xFF & array[i])).replaceAll(".*(.{8})$", "$1");
        System.out.println("1: " + s);

        byte clearedOne = (byte) (array[i] & ~(1 << 0));
        s = ("0000000" + Integer.toBinaryString(0xFF & clearedOne)).replaceAll(".*(.{8})$", "$1");
        System.out.println("2: " + s);

        if (5 > 3)
        {
          clearedOne = (byte) (array[i] & ~(1 << 0));
          s = ("0000000" + Integer.toBinaryString(0xFF & clearedOne)).replaceAll(".*(.{8})$", "$1");
          System.out.println("3: " + s);
        }
      }

    }
}
1: 11110111
2: 11110110
3: 11110110
1: 11000011
2: 11000010
3: 11000010
1: 11111010
2: 11111010
3: 11111010