0

At C code I have

record = fread(fid, 1, 'ubit32');
time = bitand(record,268435455);             %the lowest 28 bits
chan = bitand(bitshift(record,-28),15);      %the next 4 bits
markers = bitand(record,15);  % where the lowest 4 bits are marker bits

at the same at Python

recordData = "{0:0{1}b}".format(struct.unpack("<I", inputfile.read(4))[0], 32)
channel = int(recordData[0:4], base=2)
time = int(recordData[4:32], base=2)
markers = int(recordData[28:32], base=2)

I tried do it at java by bit shifting and bit mask.

byte[] record=new byte[4];
inputStream.read(record);
recordData = ((record[3] & 0xFF) << 24) | ((record[2] & 0xFF) << 16) | ((record[1] & 0xFF) << 8) | (record[0] & 0xFF);
time = (recordData >> 0) & 0xFFFF;
channel = (recordData >>  28) & 0xF;
markers = (recordData <<  28) & 0xF;

Code works good for channel and markers but for time I obtained different number. What I do wrong?

UPD

Just check the

byte[] record=new byte[4];
inputStream.read(record);
recordData = ((record[3] & 0xFF) << 24) | ((record[2] & 0xFF) << 16) | ((record[1] & 0xFF) << 8) | (record[0] & 0xFF);

line compare result with the python code by

System.out.println(Integer.toBinaryString(recordData));

result is ok for marker and chan

11110000000000000000000000000000 - for chan == 15 and marker == 0

but compare data results give

00000111010101110110100111010111 -> python
111010101110110100111010111 -> java

I think the difference only in String format print. So the read is correct.

So the main question how to obtain correct time value?

UPD

I change to the 268435455 like in C. Now I found the following:

print("%u CHN %1x %u %8.0lf\n" % (recNum, channel, timeTag, (timeTag * globRes * 1e12)))

At Java

truetime = (ofltime + timeTag) * globclock * 1e-9;
System.out.println(n + " : CHN " + channel + " " + timeTag + " " + truetime);

As result I obtain.

Java
CHN 0 123169239 8.920606556000001E-12
Python
20 CHN 0 2230151639 8920606556

TrueTime looks similar but the time is different. Can be this difference be provoke by difference of String format at python and java?

VolBog
  • 13
  • 3
  • `0xFFFF` aren't 28 bits. – tkausl Jun 11 '20 at 15:33
  • I tried `0x09000000` mask but still obtain wrong answer – VolBog Jun 11 '20 at 15:36
  • `inputStream.read(record);` ← You need to check the value returned by that method, to make sure you have read in all four bytes. You may want to consider using [DataInputStream](https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/io/DataInputStream.html) instead of just InputStream. – VGR Jun 11 '20 at 15:41
  • https://stackoverflow.com/a/473309/10871900 – dan1st Jun 11 '20 at 16:04
  • Have you tried `268435455`, which you use in C? – tkausl Jun 11 '20 at 16:08

0 Answers0