What is happening in this line of code? [duplicate] - java

This question already has answers here:
Bitwise AND (&) expression in Java
(2 answers)
Closed 7 years ago.
I came across this while playing with some random code trying to learn Java
y = (0x000000FF & ((int)x));
x = -102,
y = 154
What is exactly happening in this line?

That operation gets you the least significant 8 bits of x into y, thereby masking the 24 higher significant bits.
-102 in hex representation is ffffff9a, which would make y to be 9a.

public static void main(final String... args)
{
System.out.println(Integer.toBinaryString(-102));
System.out.println(Integer.toBinaryString(154));
}
prints:
11111111111111111111111110011010
10011010
Now write it like this instead:
11111111111111111111111110011010
00000000000000000000000010011010
Finally, 0xff is:
00000000000000000000000011111111
And you understand your result...

Related

Why casting give me these weird results Java? [duplicate]

This question already has answers here:
Why converting from float to double changes the value?
(9 answers)
Closed 2 years ago.
This is the code
public static void main(String[] args) {
double x=5.6556464566546546546556465465465;
float y=(float)x;
double z= 1+y;
System.out.println(x+"\n"+y+"\n"+z);
}
}
and this is the output
5.6556464566546545
5.6556463
6.655646324157715
I can understand the value of x and y but z from where it got those fractional numbers after the 3??!
Thank you very much
Floats are an approximation of the actual number in Java, due to the way they're stored. If you need exact values, use a BigDecimal instead.

Java readUnsignedShort() explanation [duplicate]

This question already has answers here:
What does >> do in Java?
(4 answers)
Closed 5 years ago.
Can someone explain the following code snippet, please?
methodAttributeLength = (long)dis.readUnsignedShort() << 16 | dis.readUnsignedShort();
I checked this in java doc .But I can't get the idea. I know what the java.io.DataInputStream.readUnsignedShort() method does.But the problem is that <<16 thing.
DataInputStream#readUnsignedShort return a int which spend 32 bits, but the short type spend 16 bits.
int << 16 will shift the low 16 bits to high 16 bits and fills 0 in the low 16 bits. for example:
int value = 0b11111111000000001000000011111111;
^---------------
int high = 0b10000000111111110000000000000000;
// high == value << 16
in this case, and the | operator is joins high bits and low bits together. for example:
int high = 0b10000000111111110000000000000000;
int low = 0b00000000000000001000000000000001;
int value = 0b10000000111111111000000000000001;
// value == high | low;
on the other hand, your protocol saving an int into two shorts which are a high short and a low short. and the code above is read the int value from two shorts.
for more details you can see bitewise operators and shift operators.

Why does a fraction stored in a double become a whole number upon outputting to screen? [duplicate]

This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Closed 6 years ago.
So, I have this code which is the beginning of an RSA-encrypter (current math class). Now I'm still in the beginning stages and I notice that when I try to make an array of doubles, they all come out to the screen as whole numbers, what's going on here ?
As you can see in the code I divide the multiples of 7 by 40 and it should come out as fractions (7 / 14 = 0.175 for example) but instead as 0.0.
Help
import java.util.*;
public class Encryption {
static double[] ads = new double[200];
public static void main(String args[]){
for(int i = 0; i < 200; i++){
ads[i] = (7 * i) / 40;
}
for(int i = 0; i < ads.length; i++){
System.out.println(ads[i]);
}
}
}
It's because you're storing an integer into the array in the first place.
Consider this line:
ads[i] = (7 * i) / 40;
In the expression on the right, you first multiply i by 7, then divide it by 40. i is an integer. So, let's say i == 1. Then, 7 * i == 7, which is still an integer. Both 7 and 40 are integers, so when you evaluate 7 / 40, you get integer division. Integer division always rounds down. When the result gets stored into ads[i], it gets converted to a double, but by that point it's already been rounded down.
Try changing this line to the following:
ads[i] = (7 * i) / 40.0;
This works because 40.0 is a double, not an int.
In unrelated news, if you're using double to implement RSA, you're probably doing something wrong. Doubles aren't infinitely precise, and will screw up your result.

Way to get the bit in position X of a byte array [duplicate]

This question already has answers here:
How to get the value of a bit at a certain position from a byte?
(4 answers)
Closed 7 years ago.
If I create a random byte[], for instance:
byte[] b = new byte[16];
new Random().nextBytes(b);
That means that I have 16 bytes or 128 bits of data, right?
Is there a way to read the bit in position X so that I can learn if it's 0 or 1?
This question is similar, but not the same as, an existing question that asks how to get a bit in a byte. But I want a bit in a byte array, byte[].
That means that I have 16 bytes or 128 bits of data, right?
Right!
You can get a bit in your byte array like this :
int readBit(byte[] b, int x) {
int i = x / 8;
int j = x % 8;
return (b[i] >> j) & 1;
}
In java you can not aaccess bits directly. You need to use bitwise operators.
So you could compare the byte whit a nother byte whose bit value you alleready know.

How to get a int from a long double in java? [duplicate]

This question already has answers here:
Integer division: How do you produce a double?
(11 answers)
Closed 7 years ago.
I wanna do this math in java:
int index = 3 * (9568/20001);
in my calculator it shows 3 *( 0.47837608... ) which is 1.43512824..
but, In Java that always give me 0, even I were trying use format, or java.lang.Math.round.
The first postion int 1 of 1.43512824 is what I want to get.
Try this
int index = (int)3 * (9568.0/20001);
Because an integer divided by an integer gives a integer in java thus your answer will not be accurate. If you write 9568.0/20001 it gives a double result and so result is more accurate.

Categories