Reputation: 1329
I have some C code that I am converting to java with a lot of bit level operations.
I have the following code:
float test(float uf) {
return uf ^ (1 << 31);
}
In the original C code, uf
was an unsigned value. Since java doesn't have unsigned values, I changed uf
to a float. Now, I get an error saying that the the first type is a float and the second is an int.
I know there are libraries for it, but I just want to use bit operations just to see what I can do with Java using bit operations out of curiosity.
So, my first question is, is it possible to perform the operation above using Java without using libraries? Second, are there any reasons that you should not work at a bit level in java (besides the fact that there are libraries to do it for you)?
Upvotes: 3
Views: 5835
Reputation: 4357
While you cannot directly apply bit operations to a float, it is possible to convert a float to an integer with the same bit representation (to clarify: the bits will be equal, the number value will not).
Upvotes: 5
Reputation: 2605
As other answers said, there are no bitwise operations in floating point values in Java. However, this part of your question clearly states that this is an instance of the XY problem: you are looking for the solution for Y while you actually want to solve X because you thought that Y was the best solution but now you're stuck.
In the original C code, uf was an unsigned value. Since java doesn't have unsigned values, I changed uf to a float. Now, I get an error saying that the the first type is a float and the second is an int.
Why cannot you use either a long or a normal int? It is true that common Java arithmetic operations consider the int values as signed, but there methods in java.lang.Integer that operate on two int values considering them as unsigned.
So, where in C you would do this:
unsigned int fun(unsigned int a, unsigned int b) {
unsigned int x;
scanf("%u", &x);
return a * x + b/2u;
}
In Java you would do this (note that addition and multiplication are the same operations for either signed or unsigned integers when 2-complement representation is used):
int fun(int a, int b) {
// You need to get the value as a string first, there is no nextUnsignedInt in Scanner
int x = Integer.parseUnsignedInt(new java.util.Scanner(System.in).next());
return a*x + Integer.divideUnsigned(b, 2);
}
Upvotes: 1
Reputation: 10806
That operation simply flips the 31st bit (starting from zeroth). You can do that in java just fine.
int test(int uf) {
return uf ^ (1 << 31);
}
That's your function. The problem you have isn't setting that bit, it's knowing that if that number is negative that its actually a number 0xBFFFFFFF larger than that number. So long as you are consistent with treating ints as a sequence of 32 bits rather than checking it's value at in opportune times, you'll do fine.
If you want 32 bits that give you a proper number. Use a long.
long test(int uf) {
return (uf & 0x00000000ffffffffL) ^ (1 << 31);
}
Then you can use it in comparisons properly, and simply cast it to int if you need it as int. Which if we're converting the unsigned to other types is a far superior answer than floats.
Answer 2: We should certainly use bit operations in java as they are really fast in java. Quite often in mission critical code it's best to convert over to them at the critical points, to the point of only using primitives, or even native compiled code.
Upvotes: 2
Reputation: 1575
Bitwise operations are not allowed on non-integer types in java. See this reference: http://cs-fundamentals.com/java-programming/java-bitwise-operators.php.
As far as any reason to "not work at bit level". The answer is no. There are many legitimate reasons to do bit wise operations in Java. Examples would include JavaCard applications, dealing with low level network protocols, and others.
Upvotes: 1