Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 341
  • Last Modified:

Simple Question ?

Hi;

I just wanna know of what is the use of | and ^ in java. I took an exam in java yesterday for a company.
They have questions that I dont know if I answered it correctly or not so whats the use of the symbol
above. I've reproduce a small example that I have created :

import java.awt.*;

public class Sample {

      Sample() {

      int var = 10;

      GetString(var);
      }

      private void GetString(int i) {

      System.out.println("\n");

      for(int j=1; j<=i; j++) {
      System.out.print(j);
      }

      System.out.println("\n");
      }

      public static void main(String args[]) {

      Sample s1 = new Sample();

      int x = 2 | 3;
      int y = 2;

      System.out.print("Result : ");
      System.out.println( x ^ y );
      System.out.println("Execution Successfull !");
      }
}
0
simple21
Asked:
simple21
  • 4
  • 4
  • 3
  • +2
3 Solutions
 
simple21Author Commented:
And one more question what happens to a JSP if you compile it ?
Does it turns into applet, servlet or application ormidlet ?
0
 
objectsCommented:
| and ^ are bitwise operators.
jsp->servlet
0
 
simple21Author Commented:
> bitwise operators

What are bitwise operators ? you use them for what purpose ? thanks.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
objectsCommented:
for doing binary arithmetic

eg.

0011 & 1101 = 0001
0
 
girionisCommented:
As already stated they are used when we need to do operations with bits. The | symbol is used to do an OR, which means that if one or both of the operands is 1 then the result is also 1. For example the following:

int x = 2 | 3;

will result in 3 since 2 in binary is 0001 and 3 in binary is 0011 so if we do an OR we have

    0001   = 2
    0011   = 3
--------
    0011   = 3

The first two bits of number 3 will be 0 since we do an OR on two zeros, but the last two bits will be 1 since we do an OR on zero and one (result = 1) and on one and one (result = 1).

The ^ operator is the XOR operator which returns 1 only if one of the operands is 1.
so the  x ^ y will be 3 ^ 2 which is

    0011
    0010
--------
    0001   = 1
0
 
thomas908Commented:
The Bit-Wise OR Operator: |

| | 0 1
---+-----
 0 | 0 1
 1 | 1 1

Example

   01001001
| 00011011
----------
  01011011 (This is the binary representation of 91)


The Bit-Wise XOR Operator: ^
^ | 0 1
---+-----
 0 | 0 1
 1 | 1 0

Example
01001001
^ 00011011
----------
  01010010 (This is the binary representation of 82)

For its practical use u may refer to

http://radio.javaranch.com/channel/corey/2004/06/10/1086899519000.html


>>And one more question what happens to a JSP if you compile it ?
Does it turns into applet, servlet or application ormidlet

To a servlet
0
 
thomas908Commented:
0
 
girionisCommented:
0
 
simple21Author Commented:
Im greatfull enough but I only have few points and I'll just close this questions
ones I have the points to share to all of you, thank you so much.
0
 
girionisCommented:
Take you time :)
0
 
objectsCommented:
I don't need any points if you're short
0
 
girionisCommented:
Me too :)
0
 
brettmjohnsonCommented:
| bitwise OR
^ bitwise XOR
& bitwise AND
<< bitwise left-shift
>> bitwise right-shift

so ( 2 | 3 ) would be:

0000 0010
0000 0011   OR
--------------
0000 0011


and ( x ^ y )  would be ( 3 ^ 2) which is:

0000 0011
0000 0010   XOR
--------------
0000 0001


0
 
simple21Author Commented:
Ok !.
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 4
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now