Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why int variable as it be decleared can't used " ^ " with number?

 
Hades Pan
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FOR EXP: int i=2^2;
Will print 0
 
Jim Hall
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "^" does not mean raise the number to a power. It is the exclusive or (XOR) operator.
<code>
0000 0010 // 2
0000 0010 // 2
---------
0000 0000 // 0
</code>
With XOR
0 ^ 0 -> 0
0 ^ 1 -> 1
1 ^ 0 -> 1
1 ^ 1 -> 0
Another example:
<code>
0001 0101 // 21
0010 1101 // 45
---------
0011 1000 // 56
</code>
 
Hades Pan
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got is
Thans Jim Hall~~!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic