0

the aim of the code is read data from database,but I don't know the mean of " i[1]=data[1]& 0xff;" in the getBinaryStream(1) ,what is the '1' mean?

 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;

3 Answers3

1

You are converting byte data to integer. “& 0xff” will effectively masks the variable so it leaves only the value in the last 8 bits, and ignores all the rest of the bits.

Explained very well here, What does value & 0xff do in Java?

Community
  • 1
  • 1
user2004685
  • 9,548
  • 5
  • 37
  • 54
1

Kindly refer this link for getBinaryStream(...)

Now, data[1]& 0xff mean, here with help of Bitwise AND operator('&') operation goes to perform.

More Closely speaking then,

data[1]& 0xff means, assume data[1] returns some value assume : 255

and 0xff is hexa-decimal value, it's equivalent decimal value is : 255.

so that finally this operation perform likewise,

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

Just keep in mind this for Bitwise AND operation,

1 & 1 = 1

1 & 0 = 0

0 & 1 = 0

0 & 0 = 0

Here, in your case same thing going on, i.e. Bitwise-AND operation. Let me know if any query.

Vishal Gajera
  • 4,137
  • 5
  • 28
  • 55
0

getBinaryStream() doc

Retrieves the BLOB value designated by this Blob instance as a stream.

Returns:

a stream containing the BLOB data.

The hex literal 0xFF is an equal int value 255. Java represents int as 32 bits.

Binary value of 0xFF

0xFF = 00000000 00000000 00000000 11111111

number & 0xff reference

The & operator performs a bitwise AND operation. data[1]& 0xff will give you an integer with a bit pattern.

The rule of & operator is

1&0=0

1&1=1

0&0=0

0&1=0

So when you do a & with this 0xFF on any number, it is going to make 0s all but leaves only the value in the last 8 bits.

For example, the bitwise AND of 10110111 and 00001101 is 00000101.

10110111

&

00001101

=

00000101

Community
  • 1
  • 1
SatyaTNV
  • 4,137
  • 3
  • 15
  • 31