I am extending a tool in Java that somehow is capable of applying Logical (AND, OR and XOR) over three Long values.
How is that possible? If it is possible?
You can apply bit-wise operators on longs:
& is AND
| is OR
^ is XOR
Maybe they wanted to implement IDL-like logical operators
Default Definitions of True and False
Data Type True False
====================================================================================
Byte,integer, and long Odd integers Zero or even integers
Floating point and complex Non-zero values Zero
String Any string with non-zero length Null string (" ")
Heap variables Non-null values Null values
What do you mean by not possible? 3 | 4 | 5 is a perfectly valid expression as 3 & 4 & 5.
Related
Since I started using eclipse for project euler, I noticed that big numbers sometime become a seemingly random negative numbers. I suppose this has something to do with passing the boudry of the type.
I'll be glad if you could explain to me how these negative numbers are generated and what is the logic behind it. Also, how can I avoid them (preferable not with BigInteger class). Danke!=)
This image shows what you're looking for. In your case it's obviously larger numbers, but the principle stays the same.
Examples of limits in java are:
int: −2,147,483,648 to 2,147,483,647.
long: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
In the image 0000, 0001 etc, shows the binary representation of the numbers.
EDIT: In project euler you often have to think of a way to work around the lagre numbers. The problems are designed with numbers that big so that you can't use the ordinary way of problem solving. However, if you find that you really need to use them, i suggest studying BigInteger anyway. You will find it useful in the long run, and it's not all that complicated. Here is a link with lots of understandable examples:
BigInteger Example
In mathematics numbers are infinite. However in computers they are not. There is MAX_VALUE for each int-like type: int, short, long. For example Integer.MAX_VALUE. When you try to increase number more than this value the number becomes negative. This way the internal binary representation of numbers work.
int i = Integer.MAX_VALUE;
i++; // i becomes negative.
Here's a two's complement representation for 2-bit integer: (U means Unsigned, S means Signed)
U | bits | S
---------------
0 | 00 | 0
1 | 01 | 1 \ overflow here:
2 | 10 | -2 / 1 + 1 = -2
3 | 11 | -1
Arithmetic is done mostly like in the unsigned case, modulo max(U) (4 in our case).
The logic is the same for bigger types. int in Java is 32 bit. Use long for 64 bits.
You are probably overflowing the size of your data type, since the most significant bit is the sign bit. I don't think that Java has unsigned data types, so you may try using a larger data type such as long if you want to hold bigger numbers than int. If you are still overflowing a long though, you're pretty much stuck with BigInteger.
Basically I have a number that is manipulated with bitwise operands as you can see here:
is[i_6_] = i_9_ - 256 | ~0x7fffffff;
I need to reverse/undo this bit operand..
| ~0x7fffffff
So that I would just have the value of:
i_9_ - 256r help!
How to reverse this bit operand?
1st of all im assumming the expression you wrote is actually (i_9_ - 256) | ~0x7fffffff
| ~0x7fffffff
is effectively the same as
| 0x80000000
meaning it will make the highest bit of the original number (i_9_ - 256 according to what you said) into a "1" regardless of its original value.
so i dont think you can reverse this
To just remove that bit would be, as you likely know:
& 0x7fffffff
But since you need to revert it to its previous state, you would need to--generally speaking--save the state of that most-significant bit prior to OR'ing to determine whether you should clear that bit or not. The most direct way to obtain your result, then, would be to re-use the original value of i_9_, but without the OR operation this time:
= i_9_ - 256
Using this: And with the (~) inverse of the value
i_9_ - 256 & 0x7fffffff;
inverse of inverse ~(~)
If I'm being told that a specific function will return a vector of binary flags (32-bit int value), what does that mean? Can you give an example that demonstrates that?
Thanks a lot.
A thirty-two bit integer can be viewed as a vector of thirty-two ones and zeroes, and bitwise arithmetic can be used to extract individual bits.
For example, if FLAG_FOO is an constant whose value is a power of two — say, 1024 — then flag_vector & FLAG_FOO != 0 confirms that a specific bit is set. This is because & is "bitwise and"; it evaluates to an integer whose bits are one where both operands' bits are one, and zero where either operand's bits are zero. For example, binary 00100110 & binary 10000011 is 00000010. (Except that you're using thirty-two bit integers, obviously, instead of just eight.)
Conversely, "bitwise or", |, can be used to construct such a value; for example, flag_vector = FLAG_FOO | FLAG_BAR | FLAG_BAZ would have three bits (flags) set.
This is used, for example, in the java.util.regex.Pattern class, whose static compile method is overloaded to take a second argument composed of such flags. Pattern.compile("a.c", CASE_INSENSITIVE | DOTALL) creates a pattern based on the string a.c, with the "case-insensitive" and ".-represents-any-character,-even-newline" options enabled.
This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
logical operators in java
What's the difference between | and || in Java?
As the title says, I need to know the difference between & operator and && operator.
Can anyone help me in simple words.
How do they differ from each other?
And which one to be used in a IF statement?
There are in fact three "and" operators:
a && b, with a and b being boolean: evaluate a. If true, evaluate b. If true, result is true. Otherwise, result is false. (I.e. b is not evaluated if a is not true.)
a & b, with a and b being boolean: evaluate both, do logical and (i.e. true only if both are true).
a & b, where a and b are both integral types (int, long, short, char, byte): evaluate a and b, and do a bitwise AND.
The second one can be viewed as a special type of the third one, if one sees boolean as a one-bit integral type ;-)
As the top-level condition of an if-statement you can use the first two, but the first one is most likely useful. (I.e. there are not many cases where you really need the second and the first would do something wrong, but the other way around is more common. In most cases the first is simply a little bit faster.)
If the first result is FALSE, && doesn't evaluate the second result. & does.
&& only evaluates the second expression if the first operation is true. & evaluates both expressions (even if the first expression is false and there is no point in evaluating the second expression). Hence && is a tiny bit faster than & in logical operations. Hence && is also known as short-circuit and.
& can also be used as a bitwise operator in addition to a logical operator. So you can do a 'Bit And' of 2 numbers like for example
int result = 1 & 3; // will evaluate to 1
&& cannot be used as a bit-and operator.
For conditional IF operator, use &&. It is a tiny bit faster than just &.
How does Java handle arguments separated by | ?
for example
private void foo(int i) {
System.out.println(i);
}
private void bar() {
foo(1 | 2 | 1);
}
Which would give the output
3
I've seen this used in SWT/JFace widget constructors. What I can't figure out is how the value of i is decided.
The | is a bitwise or-operator.
foo(1 | 2 | 1);
means call foo with the argument 1 bitwise-or 2 bitwise-or 1.
1 in binary is 01
2 in binary is 10
Bitwise or of 01 and 10 is 11 which is 3 in decimal.
Note that the | operator can be used for booleans as well. Difference from the || operator being that the second operand is evaluated even if the first operand evaluates to true.
Actually, all bitwise operators work on booleans as well, including the xor ^. Here however, there are no corresponding logical operator. (It would be redundant, since there is no way of doing a "lazy" evaluation of ^ :)
it is using the bitwise OR operator. For starters, 1 | 1 = 1 so the second 1 is redundant. If we remove the redundant 1 we are left with the equation 1 | 2 = 3. Looking at it in 2 bit binary it looks like:
01 | 10 = 11
The or operator will match up the corresponding bits from each or the values and if there is one 1 in either or both values for a given position, the result is a 1. If both values for both the corresponding bits then the result is 0.