SRL Archive - Computer Help and Tutorials

Operand Tutorial (Binary level)


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
they are used in 'If' statements to compare 2 items for example.

If ((Variable1 = 5) AND (Variable2 = 3)) then

Binary


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;


Waddo

2009-05-30 02:26:01

I Made this as I've been away for a while and when I came back I have the tutorial writer trophy :)

It's not finished yet.

Love you all xx

senrath

2009-05-30 02:29:10

Looks good so far. Though just for the heck of it, you should mention NAND, NOR, and XNOR.

Waddo

2009-05-30 02:32:36

I would but I dont know them my self im geussing there just NOT(AND) NOT(OR) and NOT(XOR) lol but there not as neccasary.

Naum

2009-05-31 11:57:56

Good work man, Isn't this based on the principle on Nor, Nand, Or, And, gates in physics?

Waddo

2009-06-01 17:23:28

Added XOR

Yes Nuaman this is how logic gates work and also how computers work :)

marpis

2009-06-01 19:05:40

BINARIES TO NUMBERS
for example, 1101
phase 1:
list numbers like this (from right to left)
1
0
1
1

phase 2: add there *2 to all of them
1 *2
0 *2
1 *2
1 *2

phase 3: raise all the "number 2's" we added to a power like this:
1 *2^0 = 1
0 *2^1 = 0
1 *2^2 = 4
1 *2^3 = 8

phase 4: sum these up
1 *2^0 +
0 *2^1 +
1 *2^2 +
1 *2^3
= 13

so, 1101 = 13

NUMBERS TO BINARIES
for example, 13 (to prove the former example)
13 / 2 = 6, remainder is 1
13 = 2*6+ 1
6 / 2 = 3, remainder is 0
6 = 2*3 + 0
3 / 2 = 1, remainder is 1
3 = 2*1 + 1
1 / 2 = 0, remainder is 1
1 = 2*0 + 1

then lets list the remainders from down to up:
1101
—> 1101 = 13
~~~~~~~~~~~~~~~~~~~~
another example on numbers to binaries: 257
257 = 2*128 +1
128 = 2*64 + 0
64 = 2*32 + 0
32 = 2*16 + 0
16 = 2*8 + 0
8 = 2*4 + 0
4 = 2*2 + 0
2 = 2*1 + 0
1 = 2*0 + 1
so 257 = 100000001

NOTE: REMEMBER TO COUNT THESE NUMBERS UNTIL THE RED NUMBER IS 0!!!

edit: im not sure if this is the right thread ^^

senrath

2009-06-01 19:09:04

That should really be labeled something like "Binary to Base10" and "Base10 to Binary", 'cause binary is still made up of numbers, so "Binary to Numbers" is meaningless.

marpis

2009-06-01 20:18:48

From: senrath
That should really be labeled something like "Binary to Base10" and "Base10 to Binary", 'cause binary is still made up of numbers, so "Binary to Numbers" is meaningless.


i thought binary to number is easier to understand ^^

Waddo

2009-06-03 18:39:41

Ive Included how to use XOR Swap and also linked it into Scar :)

Gu3st...

2009-06-04 00:44:26

You made binary sound like it is easy as 01, 10, 11….
Nice tut ;)

Waddo

2009-06-05 19:53:32

there are 10 types of people in the world those who understand binary and those who don't :p

Nadeem

2009-06-06 03:52:38

lol nice tut! :)

I have also made a tutorial involving how to calculate, Binary, Decimal & Hexadecimal :) ~ This stuff gets fun sometimes xD



~NS

Waddo

2009-06-06 04:19:21

I've just sat my AS computing exam I always have fun working out the algorithms and doing the binary stuff :L yes im quite sad haha but i think its awesome