Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Simple Question ?

Posted on 2004-08-16
15
Medium Priority
?
339 Views
Last Modified: 2012-05-05
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
Comment
Question by:simple21
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
  • +2
15 Comments
 

Author Comment

by:simple21
ID: 11817564
And one more question what happens to a JSP if you compile it ?
Does it turns into applet, servlet or application ormidlet ?
0
 
LVL 92

Assisted Solution

by:objects
objects earned 80 total points
ID: 11817600
| and ^ are bitwise operators.
jsp->servlet
0
 

Author Comment

by:simple21
ID: 11817765
> bitwise operators

What are bitwise operators ? you use them for what purpose ? thanks.
0
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
LVL 92

Expert Comment

by:objects
ID: 11817787
for doing binary arithmetic

eg.

0011 & 1101 = 0001
0
 
LVL 35

Accepted Solution

by:
girionis earned 80 total points
ID: 11818003
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
 
LVL 8

Assisted Solution

by:thomas908
thomas908 earned 80 total points
ID: 11818004
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
 
LVL 8

Expert Comment

by:thomas908
ID: 11818013
0
 
LVL 35

Expert Comment

by:girionis
ID: 11818015
0
 

Author Comment

by:simple21
ID: 11818223
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
 
LVL 35

Expert Comment

by:girionis
ID: 11818295
Take you time :)
0
 
LVL 92

Expert Comment

by:objects
ID: 11818315
I don't need any points if you're short
0
 
LVL 35

Expert Comment

by:girionis
ID: 11818321
Me too :)
0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 11820670
| 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
 

Author Comment

by:simple21
ID: 11938389
Ok !.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question