Bitwise Operator | and &
Page 1 of 1 • Share •
Bitwise Operator | and &
Hello Members,
How can we use these operators.
Mudassar
How can we use these operators.
Mudassar
mudassar- Posts: 1
Join date: 2008-03-03
Re: Bitwise Operator | and &
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
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
Permissions of this forum:
You can reply to topics in this forum





