This question already has answers here:
What does this sign exactly mean? |=
(5 answers)
Closed 8 years ago.
this is a function for changing bit value of image. what does |= and ^= mean?
private int setBitValue(int n,int location,int bit) {
int toggle=(int)Math.pow(2,location),bv=getBitValue(n,location);
if(bv==bit)
return n;
if(bv==0 && bit==1)
n|=toggle; // what does it do?
else if(bv==1 && bit==0)
n^=toggle; // what does it do?
return n;
}
Its the same short form as in +=
n |= toogle
is the same as
n = n | toogle
the | is here the binary-or operator
and ^ is the binary xor-operator
They are short hand assignment operations.
n|=toggle; is equivalent to n=n|toggle;
and
n^=toggle; is equivalent to n=n^toggle;
And
| is bitwise OR
^ is bitwise XOR
They're the bitwise OR equals and the bitwise XOR equals operators. They are mainly used for dealing with bit flags. I highly recommend this article if you want to learn more about bitwise and bit-shifting operations.
These are shorthand bitwise operators. Like += using |= is the same as:
a = a | b;
Read the Oracle Documentation about Bitwise and Bit Shift Operators for further information.
Related
This question already has answers here:
How do shift operators work in Java? [duplicate]
(9 answers)
Closed 4 years ago.
Output is 20 but i am not getting the logic behind this.Please anyone explain
public static void main(String[] args)
{
System.out.println(5<<2);
}
If you read about Bitwise and Bit Shift Operators:-
The unary bitwise complement operator "~" inverts a bit pattern; it can be applied to any of the integral types, making every "0" a "1" and every "1" a "0". For example, a byte contains 8 bits; applying this operator to a value whose bit pattern is "00000000" would change its pattern to "11111111".
The signed left shift operator "<<" shifts a bit pattern to the left, and the signed right shift operator ">>" shifts a bit pattern to the right. The bit pattern is given by the left-hand operand, and the number of positions to shift by the right-hand operand. The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension.
The bitwise & operator performs a bitwise AND operation.
The bitwise ^ operator performs a bitwise exclusive OR operation.
The bitwise | operator performs a bitwise inclusive OR operation.
The following program, BitDemo, uses the bitwise AND operator to print the number "2" to standard output.
class BitDemo {
public static void main(String[] args) {
int bitmask = 0x000F;
int val = 0x2222;
// prints "2"
System.out.println(val & bitmask);
}
}
This question already has answers here:
Explain the use of a bit vector for determining if all characters are unique
(12 answers)
Closed 4 years ago.
I have the following code that finds unique characters in a string using bit vectors. We assume it's an ASCII char set with lower case letters only.
I am having a hard time understanding the use of bit vectors below. Even after debugging through the program and following the changes variables go through after each loop.
// assuming that the characters range from a-z
static boolean isUniqueBitVector(String str) {
int checker = 0;
for(int i = 0; i < str.length(); i++) {
int val = str.charAt(i) - 'a';
if((checker & (1 << val)) > 0) {
return false;
} else {
checker |= (1 << val);
}
}
return true;
}
What's the purpose of left shifting the val(int representation of each char in string) by 1 and AND'ing it with checker (initialized to 0) and OR'ing it in the else block.
checker is a 32 bit integer, of which we assign the lowest 26 to store a flag for each letter a-z. We can use bits for this because a letter can be non-unique only once.
int val = str.charAt(i) - 'a'; computes the index of the bit that corresponds to the current letter. This is where the assumption that the input only contains a-z comes in. val will be a number between zero and 25.
In general, (1 << val) is the bit corresponding to the selected letter. << is the left shift operator. It is used to move 1, which only has a single bit on, val positions to the left. So for example, 1<<3 would be 8. In fact, left shifting is equivalent to multiplying by a power of 2.
(checker & (1 << val)) verifies if the selected bit is on or not. The & operator is called bitwise and. It combines two numbers and sets any bits that are on in both to on. Remember that 1 << val only has a single bit turned on. The only way the result of that and checker is going to be nonzero is if checker already has the same bit turned on. In that case we return false because a letter has been repeated.
In the case that a new letter has been found, we need to turn on the correct bit in checker. This is what checker |= (1 << val); does. The bitwise or operator, |, turns on a bit in the result if it is on in either operand. Again, 1 << val has only one bit on. Therefore the result of the or is to replicate checker and turn on that one bit (whether or not it was already on).
All of the operations you see here are very common idioms. Hopefully my explanation has helped you in some way, because you will almost certainly see very similar things in any simple bit twiddling code.
I was looking source code of a java project, herein i found an operator |= for boolean variables.
Can anybody tell me what exactly is this? and best way to use this.
Thanks for reply, now improving my question: what |= shorthand operator actually does. as per my test it shows:
false | true = true
false | false = false
true | false = true
true | true = true
But i still not clear, how it determines the result. And any use case where i can use this.
Thanks
a |= b; means the same as a = a | b;, in the same way that a += b; means the same as a = a + b;.
You would use it whenever you have something of the form a = a | b; (which is rare) and want to shorten it.
As you have got your answer that it is a short and a compound assignment operator. So if you write
a1 |= a2;
or
a1 = a1 | a2;
the both mean the same thing. Its just the way to write the code.
Regarding the two W's which you have asked i.e, when to use and why to use? is completely dependent upon the programmer as some programmer finds the first one as more readable and some find the latter.
Here is a list of other such operators.
Operator Description Example
----------------------------------------------------------------------------------------
|= bitwise inclusive OR and assignment operator C |= 2 is same as C = C | 2
^= bitwise exclusive OR and assignment operator C ^= 2 is same as C = C ^ 2
&= Bitwise AND assignment operator C &= 2 is same as C = C & 2
<<= Left shift AND assignment operator C <<= 2 is same as C = C << 2
>>= Right shift AND assignment operator C >>= 2 is same as C = C >> 2
----------------------------------------------------------------------------------------
It's a short form for an assignment doing a logic OR with the left-hand operand.
a|=b ---> a=a|b
You can do the same thing with other operators: +, -, *, &, ^ , etc.
Update:
Adding something because it seems you need a short explanation of the logical or: This operator returns true only if at least one of its boolean operands is true. See this page on wiki.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
^ operator in java
I was assuming that c ^ d is a calculation like 'the power of', so c = 5, d = 2, result is 25. I think I'm wrong, though.
Can you explain what (c ^ d) does in java, for example in
result = result + (char)(c ^ d)
^ is the bitwise xor meaning that 0b0101^0b0010 (5^2) is 0b0111 and 0b0101^0b0111 is 0b0010
look at the truth table of xor (the result is 1 if the input are different
a b | a^b
---------
0 0 | 0
0 1 | 1
1 0 | 1
1 1 | 0
the bitwise operators take each bit of the terms and apply the operator to each bit
The ^ operator performs a Bitwise Exclusive OR.
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
For raising a number to a power, you would use the Math.pow function.
That one is the bitwise XOR operator.
^ is a bitwise XOR.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Weird java behavior with casts to primitive types
Let's look at the following code snippet in Java.
package typecasting;
final public class TypeCasting
{
public static void main(String[] args)
{
int i = (byte) + (char) - (int) + (long) - 1;
System.out.print("\n i = "+i+"\n");
}
}
The statement System.out.print("\n i = "+i+"\n"); displays i = 1. How?
This code uses the unary + and - operators.
It's equivalent to -(-1) with a bunch of extra casts. (the unary + operator doesn't change the value)
The line:
(byte) + (char) - (int) + (long) - 1;
is being parsed as:
(byte) (+(char) (-(int) (+(long)(-1) ) ) );
All the + and - are unary operators. Since there are two -, the 1 gets negated twice so the entire expression evaluates to 1.
The operators are being treated as unary + and unary -.
int i = (byte) (+ (char) (- (int) (+ (long) (- 1))));
The JLS specifies the following:
CastExpression:
( PrimitiveType Dimsopt ) UnaryExpression
( ReferenceType ) UnaryExpressionNotPlusMinus
This means that any expression that is (Primitive Type) is followed by a UnaryExpression. If I take your statement and put [] around the unary expressions:
(byte)
[+(char)
[-(int)
[+(long)
[-1]
]
]
];