|
| ||||||||||
| Tags: java, operator, xor |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| Explain java xor (^) operator
I am just a bit confused using XOR (^) in java. Is there any example for using or explaining this operator? Thanks in advance. |
|
#2
| |||
| |||
| Re: Explain java xor (^) operator xor ^ is both a both boolean and bitwise operator in Java. It gives the bitwise difference. e.g. Code: 0b1100_0000 ^ 0b1010_0001 -> 0b0110_0001 Conditional Toggle Consider a conditional toggle like this: Code: if ( condition )
{
toggle = ! toggle;
} Code: toggle ^= condition; |
|
#3
| ||||
| ||||
| Re: Explain java xor (^) operator Java Bitwise XOR "^" Operator The Java programming language has operators that perform bitwise operations. Description of code: The bitwise XOR "^" operator produces 1 if both of the bits in its operands are different. However, if both of the bits are same then this operator produces 0. Moreover if both of the bits are 1 i.e. 1^1 then also it produces 1. The corresponding bits of both operands are 1 and 0, hence we get 1 as output because the bits are different. Code: class BitwiseXOR{
public static void main(String args[]){
System.out.println(" ^ XOR operator");
int x = 1 ^ 0;
System.out.println("1 ^ 0 = " + x);
}
} Code: C:\unique>javac BitwiseXOR.java C:\unique>java BitwiseXOR ^ XOR operator 1 ^ 0 = 1 C:\unique> |
|
#4
| ||||
| ||||
| Re: Explain java xor (^) operator Graphic Example Code: import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class XORRectangles extends JPanel{
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
// using white as the XOR color.
g2.setXORMode(Color.white);
// Paint a red rectangle.
Rectangle2D r = new Rectangle2D.Double(50, 50, 150, 100);
g2.setPaint(Color.red);
g2.fill(r);
g2.transform(AffineTransform.getTranslateInstance(25, 25));
// Draw a blue rectangle.
g2.setPaint(Color.blue);
g2.fill(r);
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.getContentPane().add(new XORRectangles());
f.setSize(300, 200);
f.setVisible(true);
}
} |
|
#5
| |||
| |||
| Re: Explain java xor (^) operator
The operator ^ performs the exclusive OR operation bit (XOR). XOR function applied on each pair of bits of equal weight is to each operand. The XOR function is evaluated to true if the operands have the same value. Finally, the complement operator inverts the value of each bit of the operand. In. NET Framework, the equivalent class, System.Decimal, can not perform logical operations. The XOR operator (^) can not be applied to an operand System.Decimal. |
|
#6
| |||
| |||
| Re: Explain java xor (^) operator
The bitwise XOR "^" operator in JAva is use to perform bitwise operations. The operator here helps you to perform bitwise and bit shift operations. This are less used in java. So it is recommended that you must go with a brief description of this. There are sample script and files on web that you can try to learn about this. |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "Explain java xor (^) operator" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| The way to operator overloading in Java | Kohlmann | Software Development | 8 | 18-09-2010 09:57 PM |
| Explain main method in java | Angelica Maria | Software Development | 5 | 10-03-2010 12:18 PM |
| How to use the ternary operator in java program? | Kasper | Software Development | 4 | 23-01-2010 06:08 PM |
| Explain Try Catch Block in java | cyber-noob | Software Development | 3 | 16-11-2009 01:13 PM |
| explain class in java | GlassFish | Software Development | 3 | 03-11-2009 08:59 AM |