【问题标题】:I don't know the mean of " i[1]=data[1]& 0xff;" [duplicate]我不知道“ i[1]=data[1]& 0xff;”的意思[复制]
【发布时间】:2016-01-30 15:23:09
【问题描述】:

代码的目的是从数据库中读取数据,但我不知道“i[1]=data[1]& 0xff;”的意思在 getBinaryStream(1) 中,“1”是什么意思?

 if(rs.next())
            {

                InputStream in=rs.getBinaryStream(1);//what the mean of the code?
                byte[] data = StreamTool.readInputStream(in);

                 int[] i =new int[12];
                 i[1]=data[1]& 0xff;//what the mean of the code?
                 i[4]=data[4]& 0xff;//what the mean of the code?
                 i[7]=data[7]& 0xff;//what the mean of the code?
                 i[10]=data[10]& 0xff;//what the mean of the code?
                 int a=3*(port1-1)+1;
                 int b=3*(port2-1)+1;

【问题讨论】:

    标签: java mysql


    【解决方案1】:

    您正在将字节数据转换为整数。 “& 0xff” 将有效地屏蔽变量,因此它只留下最后 8 位中的值,并忽略所有其余位。

    这里解释的很好, What does value & 0xff do in Java?

    【讨论】:

      【解决方案2】:

      请参考此link 获取 getBinaryStream(...)

      现在,data[1]& 0xff 的意思是, 在Bitwise AND operator('&') operation goes to perform.的帮助下在这里

      然后更仔细地说,

      data[1]& 0xff 表示,假设 data[1] 返回一些值,假设:255

      0xff is hexa-decimal value,其等效十进制值为:255

      所以最后这个操作同样执行,

        255 (11111111)
       &255 (11111111)
      ----------- 
      11111111 : 255(decimal) 
      

      对于按位与运算,请记住这一点,

      1 & 1 = 1

      1 & 0 = 0

      0 & 1 = 0

      0 & 0 = 0

      在这里,在您的情况下,同样的事情正在发生,即按位与运算。 如果有任何疑问,请告诉我。

      【讨论】:

        【解决方案3】:

        getBinaryStream() doc

        检索此 Blob 实例指定的 BLOB 值作为流。

        返回:

        包含 BLOB 数据的流。

        十六进制文字 0xFF 是一个相等的 int 值 255。Java 将 int 表示为 32 位。

        0xFF的二进制值

        0xFF = 00000000 00000000 00000000 11111111

        number & 0xff reference

        & 运算符执行bitwise AND 运算。 data[1]& 0xff 会给你一个带有位模式的整数。

        &运算符的规则是

        1&0=0

        1&1=1

        0&0=0

        0&1=0

        因此,当您在任何数字上使用此 0xFF 执行 & 时,它将使 0s 全部变为但仅保留最后 8 位中的值。

        例如,1011011100001101 的按位与是 00000101

        10110111

        &

        00001101

        =

        00000101

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-05-24
          • 2021-05-27
          • 1970-01-01
          • 1970-01-01
          • 2013-08-15
          • 1970-01-01
          • 2015-05-24
          相关资源
          最近更新 更多