Bitwise Operator | and &

Post new topic   Reply to topic

View previous topic View next topic Go down

Bitwise Operator | and &

Post  mudassar on Mon Mar 17, 2008 11:05 am

Hello Members,
How can we use these operators.
Mudassar

mudassar

Posts: 1
Join date: 2008-03-03

View user profile

Back to top Go down

Re: Bitwise Operator | and &

Post  saadia on Mon Mar 17, 2008 11:23 am

Dear Mudassar,
To solve these operators you must first of all now the truth tables of both operators

Let you have two operands A and B


Evaluation of & operator

A B A & B
0 0 0
0 1 0
1 0 0
1 1 1

Evaluation of | operator


A B A | B
0 0 0
0 1 1
1 0 1
1 1 1


It means you need to have bits that is binary representation of your values
So

Decimal Binary
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
10 1010
11 1011


Now consider following examples

1. System.out.println("3 & 3 " +( 3 & 3));
3 & 3
Binary of 3 = 0011
0 0 1 1
&
0 0 1 1
_______
0 0 1 1
0011 is the binay of 3 so result of System.out.println("3 & 3 = " +( 3 & 3)); is “3 & 3= 3"

2. System.out.println("3 & 6 "+(3 & 6));

Binary of 3 = 0011
Binary of 6 = 0110
0 0 1 1
&
0 1 1 0
_______
0 0 1 0
0 0 1 0 is binary of 2 so result of System.out.println("3 & 6 = "+(3 & 6)); is “3 & 6 = 2”

3. System.out.println( 3 | 7);
Binary of 3 = 0011
Binary of 7 = 0111
0 0 1 1
&
0 1 1 1
_______
0 1 1 1
0 1 1 1 is binary of 7 so result of System.out.println( 3 | 7); is “7”

I hope this will be helpfull
Saadia

saadia

Posts: 1
Join date: 2008-03-17

View user profile

Back to top Go down

View previous topic View next topic Back to top


Post new topic   Reply to topic
Permissions of this forum:
You can reply to topics in this forum