I have following java program:
public class java {
public static void main(String[] args) {
byte a=64, b;
int i;
i=a<<2;
b=(byte)(a<<2);
System.out.println(i);
System.out.println(b);
}
}
In this program, how the value of b is zero? I didn't get it.
Because a byte is exactly 8 bits. And the last 8 bits of your int are 0. If we add the result of Integer.toBinaryString(int) like,
byte a = 64;
int i = a << 2;
System.out.println(Integer.toBinaryString(i));
byte b = (byte) (a << 2);
you'll see that the output is
100000000
so b (because the 1 is the ninth bit) becomes
00000000
(which is 0).
Related
I have to build a serial comunication app using python, the old app is only working on windows xp and was written in C. Now i have to switch to linux and i dont have a working driver. I started to code it myself. I got the protocol from the producing company of the serial device.
The serial device receives and sends data wich is ended by a CRC. I'm new to python and i dont have a solution for this, maybe someone can help me.
This is the CRC alghoritm:
ALGORITHM FOR CRC CALCULATION
The two CRC bytes are calculated according to the formula x^15 + 1. In the calculation are included all data bytes plus the byte for block end. Every byte passes through the calculation register from teh MSB to LSB.
Three working bytes are used - S1, S0 and TR
S1 - Most significant byte from the CRC ( it is transmitted immediatelly after END)
S0 - Least significant byte from the CRC ( It is transmitted after S1)
TR - the current transmitted byte in the block.
The CRC is calculated as follows:
1. S1 and S0 are zeroed
2. TR is loaded with the current transmitted byte. The byte is transmitted.
3. Points 3.1 and 3.2 are executed 8 times:
3.1. S1, S0 and TR are shifted one bit to the left.
3.2. If the carry bit from S1 is 1, the MSB of S1 and LSB of S0 are inverted.
Points 2 and 3 are executed for all bytes, included in the calculation of the CRC - from the first byte after BEG up to and including byte END.
4. TR is loaded with 0 and point 3 is executed
5. TR is loaded with 0 and point 3 is executed
6. Byte S1 is transmitted
7. Byte S0 is transmitted
ALGORITHM FOR CRC CHECK ON RECEIVING
Three working bytes are used S1, S0 and RC
S1 - Most significant byte from the CRC ( it is received immediately after END)
S0 - Least significant byte from the CRC ( transmitted after S1)
RC - the current received byte in the block ( beginning from the first byte after BEG and ending 2 bytes after END).
The CRC is obtained as follows:
1. S1 and S0 are zeroed
2. RC is loaded with the current received byte
3. Points 3.1 and 3.2 are executed 8 times:
3.1. S1, S0 and RC are shifted 8 times to the left
3.2. if the MSB of S1 is 1 then MSB of S1 and LSB of S0 are inverted.
Points 2 and 3 are executed for all bytes, included in the calculation of the CRC - from the first byte after BEG up to and including 2 bytes after END.
S1 and S0 must be 0.
If there is someone wich can show me how to do it i'll be very gratefull.Thank you all.
EDIT 1:
I managed to get the same CRC procedure made by someone, but its made in java, i'm not that good with java. Maybe you can guide me into converting it in python. This is the code:
public class Crc {
public static final String CRC_NAME = "CRC-16-ECR";
private static final int POLYNOMIAL = 32769;
public static final int WIDTH = 16;
public static final int TOPBIT = 32768;
short CRC;
short[] crcTable = new short[256];
public Crc() {
this.crcInit();
}
private void crcInit() {
for(int dividend = 0; dividend < 256; ++dividend) {
int remainder = dividend << 8;
for(byte bit = 8; bit > 0; --bit) {
if((remainder & 'θ') != 0) {
remainder = (remainder << 1 ^ 'θ') & '\uffff';
} else {
remainder = remainder << 1 & '\uffff';
}
}
this.crcTable[dividend] = (short)remainder;
}
}
public short crcFast(byte[] message, int nBytes) {
int remainder = 0;
for(int oneByte = 0; oneByte < nBytes; ++oneByte) {
int data = (message[oneByte] ^ remainder >> 8) & 255;
remainder = this.crcTable[data] ^ remainder << 8;
}
return (short)remainder;
}
}
#!/usr/bin/python
import sys
crc = 0
while True:
ch = sys.stdin.read(1)
if not ch:
break
crc ^= ord(ch) << 8
for _ in range(8):
crc = crc << 1 if (crc & 0x8000) == 0 else (crc << 1) ^ 0x8001
crc &= 0xffff
print(format(crc, '04x'))
Is this the correct way to bit shift into a char?
char value = (char)((array[offset] << 9) + (array[offset + 1]));
If not, please correct me.
Bytes in Java are a bit tricky.
They don't go from 0 to 255 like you might be used to from other languages. Instead, they go from 0 to 127 and then from -128 to -1.
This means, that all bytes in your byte array that is below 128 will be converted into the correct char with this code:
char value = (char)((array[offset] << 8) + (array[offset + 1]));
But if the byte value is above 127, you'll probably get results you didn't expect.
Small example:
class test {
public static void main(String[] args) {
byte b = (byte)150;
char c = (char)b;
int i = (int)c;
System.out.println(b);
System.out.println(c);
System.out.println(i);
}
}
Will output this:
-106
οΎ
65430
Not exactly what you might expect. (Depending of course how well you know Java).
So to properly convert 2 bytes into a char, you'd probably want a function like this:
char toChar(byte b1, byte b2) {
char c1 = (char)b1;
char c2 = (char)b2;
if (c1<0) c1+=256;
if (c2<0) c2+=256;
return (char)((c1<<8)+c2);
}
I don't understand what is this doCalculatePi means or does, in the following example:
public static double doCalculatePi(final int sliceNr) {
final int from = sliceNr * 10;
final int to = from + 10;
final int c = (to << 1) + 1;
double acc = 0;
for (int a = 4 - ((from & 1) << 3), b = (from << 1) + 1; b < c; a = -a, b += 2) {
acc += ((double) a) / b;
}
return acc;
}
public static void main(String args[]){
System.out.println(doCalculatePi(1));
System.out.println(doCalculatePi(2));
System.out.println(doCalculatePi(3));
System.out.println(doCalculatePi(4));
System.out.println(doCalculatePi(10));
System.out.println(doCalculatePi(100));
}
I have printed the values to understand what the results are but I still have no clue what this code calculates. The conditions inside the loop are not clear.
<< means left shift operation, which shifts the left-hand operand left by the number of bits specified by the right-hand operand (See oracle docs).
Say, you have a decimal value, 5 which binary representation is 101
Now for simplicity, consider,
byte a = (byte)0x05;
Hence, the bit representation of a will be,
a = 00000101 // 1 byte is 8 bit
Now if you left shift a by 2, then a will be
a << 2
a = 00010100 //shifted place filled with zero/s
So, you may now understand that, left shift a by 3 means
a << 3
a = 00101000
For better understanding you need to study Bitwise operation.
Note, you are using int instead of byte, and by default, the int data type is a 32-bit signed integer (reference here), so you have to consider,
int a = 5;
in binary
a << 3
a = 00000000 00000000 00000000 00101000 // total 32 bit
My guess is that it approximates PI with
PI = doCalculatePi(0)+doCalculatePi(1)+doCalculatePi(2)+...
Just a guess.
Trying this
double d = 0;
for(int k = 0; k<1000; k++) {
System.out.println(d += doCalculatePi(k));
}
gives me
3.0418396189294032
3.09162380666784
3.1082685666989476
[...]
3.1414924531892394
3.14149255348994
3.1414926535900394
<< is the Bitshift operator.
Basically, every number is represented as a series of binary digits (0's and 1's), and you're shifting each of those digits to the left by however many places you indicate. So for example, 15 is 00001111 and 15 << 1 is 00011110 (or 30), while 15 << 2 is (00111100) which is 60.
There's some special handling that comes into play when you get to the sign bit, but you should get the point.
Say you are having a byte of pattern:
byte b = 0x%1;
How to tell when a byte does have certain values on the "2nd position" - in place of % ?
In this example 1, no matter what the 1st position holds.
Use a mask bits to get the last 8 bits:
int last8bits = b & 0xF;
Edit: You should read up on bitwise operations.
Full example:
public static void main(String[] args) {
byte b = (byte) 0xA1;
int last8bits = b & 0xF;
if (last8bits == 0x01)
System.out.println("\"matches\"");
}
if ((0x0F & b) == 0x01) {
// pattern matched
I have a binary number and i want to get the decimal value of only the seven bits and not include the 8th bit. How do i go about this in java.
eg. 130 as binary => 10000010, I only need 00000010 =>2 ,ie, change only the most significant bit to 0.
Please help.
Use a bit-mask:
int y = x & 0x7F;
byte b =10;
byte result = (byte) (b & 127);
Under the cover it would be
00001010 //10 in dec
AND 01111111 // our mask ,127 in dec
= 00001010 //10
another example
10000001 //129 in dec
AND 01111111 // our mask ,127 in dec
= 00000001 //1
private static boolean getBit(int b, int p) {
int mask = 1 << 8 - p;
return (b & mask) > 0;
}
private static int setBit(int b, int p) {
int mask = 1 << 8 - p;
return b | mask;
}
private static int unsetBit(int b, int p) {
int mask = 1 << 8 - p;
return b & ~mask;
}