Waddo
2009-05-30 02:22:07
What is an Operand
There are a few Operands but ill go into only the main 4,
- AND
- OR
- NOT
- XOR
If ((Variable1 = 5) AND (Variable2 = 3)) thenBinary
Ok so this is a binary level tutorial so I will teach some binary.
We will work with 8 bit binary at the moment this isa number that contains 8 bits this is 1 byte.
??????
Whats a Bit?
A bit is a value that can ether be 0 or 1 kind of like a light can be on or off.
Whats a Byte?
A Byte is 8 Bits eg. 01001101
??????
What do they mean?
Each Byte represents a number the lowest being 0 the highest being 255.
eg 00000000 = 0
11111111 = 255
How does that work?
Each bit is a number which doubles as it goes from right to left.
128 64 32 16 8 4 2 1
0 0 0 0 0 0 0 0
so 00101001 = 42
and 10000000 = 128
and 00000001 = 1
What does 01011110 equal?
Highlight Here for answer.
94
Highlight Here for answer.
Try doing it in reverse work out these in binary.
20
200
5
42
Highlight Here for answer.
00010100
11001000
00000101
00101010
Highlight Here for answer.
Operands
Each Operand has rules which i will explain in the following.
AND
The Rules -
If A is true AND B is true then C is true.
How you do it.
A, B and C are bits C is the result.
if Bit A is 1 and Bit B is 1 then Bit C = 1
Truth Table.
A truth table lists all the possibilities and the outcome's.
The AND Truth Table-
Bit A | Bit B | Bit C
0 | 0 | 0
0 | 1 | 0
1 | 1 | 1
eg. 1 AND 0 = 0
1 AND 1 = 1
If we do this with 2 Bit Binary we work vertically 1 by one.
10 AND 11 (Note this is not ten and eleven this is ONE ZERO and ONE ONE)
List them vertically
10
11
Carry out an AND on each vertical set
1 AND 1 = 1
0 AND 1 = 0
there fore C = 10
Most computer work with 8 Bit Binary, so
10011011 AND 00110011
would be
A 10011011
B 00110011
C 00010011
Try work out
10101010 AND 11110000
Hint use the Truth Table.
The AND Truth Table-
Bit A | Bit B | Bit C
0 | 0 | 0
0 | 1 | 0
1 | 1 | 1
Highlight Here for answer.
10100000
Highlight Here for answer.
Remember these Bytes stand for numbers
So try
50 AND 23
First convert them to binary
Then Perform the AND then convert them back to numbers.
Highlight Here for answer.
18
Highlight Here for answer.
OR
The Rules -
If A OR B is true then C is true.
How you do it.
if Bit A is 1 or Bit B is 1 then Bit C = 1
Truth Table.
A truth table lists all the possibilities and the outcome's.
The OR Truth Table-
Bit A | Bit B | Bit C
0 | 0 | 0
0 | 1 | 1
1 | 1 | 1
eg. 1 OR 0 = 1
0 OR 0 = 0
10011011 OR 00110011
would be
Bit A 10011011
Bit B 00110011
—————
Bit C 10111011
Try work out
10101010 OR 11110000
again use the Truth Table.
The AND Truth Table-
Bit A | Bit B | Bit C
0 | 0 | 0
0 | 1 | 1
1 | 1 | 1
Highlight Here for answer.
11111010
Highlight Here for answer.
Remember these Bytes stand for numbers
So try
50 OR 23
First convert them to binary
Then Perform the OR then convert them back to numbers.
Highlight Here for answer.
55
Highlight Here for answer.
NOT
This is the simplest operand it simply swaps the Bit eg 0 becomes 1 and 1 becomes 0.
It is the only Operand that uses only 1 Byte.
eg. NOT 00011100 =11100011
Try this.
NOT 00010111
Highlight Here for answer.
11101000
Highlight Here for answer.
and with a real number.
26
Highlight Here for answer.
00011010
11100101
128 64 32 5
so the answer is 229
Highlight Here for answer.
XOR
The Rules -
If either A OR B is true but NOT both then C is true.
How you do it.
if either Bit A or Bit B is 1 but not both of them then Bit C = 1
Truth Table.
A truth table lists all the possibilities and the outcome's.
The XOR Truth Table-
Bit A | Bit B | Bit C
0 | 0 | 0
0 | 1 | 1
1 | 1 | 0
eg. 1 XOR 0 = 1
1 XOR 1 = 0
10011011 XOR 00110011
would be
Bit A 10011011
Bit B 00110011
—————
Bit C 10101000
Try work out
10101010 XOR 11110000
again use the Truth Table.
The XOR Truth Table-
Bit A | Bit B | Bit C
0 | 0 | 0
0 | 1 | 1
1 | 1 | 0
Highlight Here for answer.
01011010
Highlight Here for answer.
Remember these Bytes stand for numbers
So try
50 XOR 23
First convert them to binary
Then Perform the XOR then convert them back to numbers.
Highlight Here for answer.
37
Highlight Here for answer.
The XOR swap
The XOR swap enables 2 variables to be swapped without using a 3rd variable
Scar code to swap 2 variables(without using XOR)
program Swap;
var
A,B,Store: Integer;
Procedure Setup;
begin
A:=5;
B:=42;
end;
begin
setup;
writeln('A='+inttostr(A)+' B='+inttostr(B));
Store:=A;
A:=B;
B:=store;
writeln('A='+inttostr(A)+' B='+inttostr(B));
end.With the XOR Swap the 3rd variable is not needed.
program Swap;
var
A,B: Integer;
Procedure Setup;
begin
A:=5;
B:=42;
end;
begin
setup;
writeln('A='+inttostr(A)+' B='+inttostr(B));
A:=A XOR B;
B:=A XOR B;
A:=A XOR B;
writeln('A='+inttostr(A)+' B='+inttostr(B));
end.to show how it works we will run through it.
A:=5;
B:=42;
A:=A XOR B;
B:=A XOR B;
A:=A XOR B;
so we will convert 5 and 42 to binary.
5 = 00000101
42 = 00101010
so A = 00000101
and B = 00101010
A:=A XOR B;
00000101 XOR
00101010
this would make
A=00101111
B:=A XOR B; remember to use what we just worked out A is now 00101111
00101111 XOR
00101010
so this means
B=00000101
and once more remembering to use the new B and the new A
A:=A XOR B;
00101111 XOR
00000101
this would make
A=00101010
So at the end we have
A=00101010
B=00000101
lets convert these back to numbers
A:=42;
B:=5;
and at the start we had
A:=5;
B:=42;