0% found this document useful (0 votes)
44 views

Binary Codes Bs Psych 1 Y1 2

Uploaded by

keisha109554
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Binary Codes Bs Psych 1 Y1 2

Uploaded by

keisha109554
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 109

BINARY CODES

Binary Code

A binary code is just an assignment


of information to bit patterns.
Using Binary
Coding Information
Remember
 Bit = 0 or 1, Binary Digit
 Byte = the number of bits used to represent
letters, numbers and special characters such
as $ # , / &.
 Word = number of bytes a computer can
process at one time by the CPU.

So,
Bits form Bytes and Bytes form Words.

Copyright © 2008 by Helene G. Kershner


Using Binary
Coding Information
Two common formats for coding letters, numbers
and special characters are:
 EBCDIC -- Extended Binary Coded Decimal
Interchange Code
 8 bit code
 Originally used in IBM mainframes
 ASCII -- American Standard Code for
Information Interchange
 7 bit code
 Originally used on non-IBM systems
Alphanumeric Data
Problem: Distinguishing between the
number 123 (one hundred and twenty-
three) and the characters “123” (one,
two, three) 1 2 3
Four standards for representing letters
(alpha) and numbers
BCD – Binary-coded decimal
ASCII – American standard code for
information interchange
EBCDIC – Extended binary-coded decimal
interchange code
Unicode
Standard Alphanumeric Formats
• BCD Next 2 slides

• ASCII
• EBCDIC
• Unicode
Binary-Coded Decimal (BCD)
Digit
• Four bits per digit 0 0000
Note: the following bit 1 0001
patterns are not used: 2 0010
3 0011
1010
4 0100
1011
1100 5 0101
1101 6 0110
1110 7 0111
1111 8 1000
9 1001
Example
• 709310 = ? (in BCD)
7 0 9 3

0111 0000 1001 0011


Standard Alphanumeric Formats
• BCD
• ASCII Next 22 slides

• EBCDIC
• Unicode
The Problem
• Representing text strings, such as
“Hello, world”, in a computer
Codes and Characters
• Each character is coded as a byte
• Most common coding system is ASCII
(Pronounced ass-key)
• ASCII = American National Standard Code for
Information Interchange
• Defined in ANSI document X3.4-1977
ASCII Features
• 7-bit code
• 8th bit is unused (or used for a parity bit)
• 27 = 128 codes
• Two general types of codes:
– 95 are “Graphic” codes (displayable on a console)
– 33 are “Control” codes (control features of the
console or communications channel)
ASCII Chart
Most significant bit

Least significant bit


e.g., ‘a’ = 1100001

Introduction to Information
Technologies
95 Graphic codes
33 Control codes
Alphabetic codes
Numeric codes
Punctuation, etc.
“Hello, world” Example

Binary Hexadecimal Decimal

H = 01001000 = 48 = 72
e = 01100101 = 65 = 101
l = 01101100 = 6C = 108
l = 01101100 = 6C = 108
o = 01101111 = 6F = 111
, = 00101100 = 2C = 44
= 00100000 = 20 = 32
w = 01110111 = 77 = 119
o = 01100111 = 67 = 103
r = 01110010 = 72 = 114
l = 01101100 = 6C = 108
d = 01100100 = 64 = 100
Decimal, Binary, and
Hexadecimal conversion
Decimal, Binary, and Hexadecimal conversion
Decimal Binary Hexadecimal
0 0000 0
1 0001 1
2 0010 2
3 0011 3
4 0100 4
5 0101 5
6 0110 6
7 0111 7
8 1000 8
9 1001 9
10 1010 A
11 1011 B
12 1100 C
13 1101 D
14 1110 E
15 1111 F
Binary-to-Decimal Conversion
The decimal value of any binary number can be
found by adding the weights of all bits that are 1
and discarding the weights of all bits that are 0.
Example
Convert the binary whole number 101101 to
decimal.
32 16 8 4 2 1
Binary no: 1 0 1 1 0 1
101101= 32+8+4+1=45
Binary-to-Hexadecimal Conversion
Simply break the binary number into 4-bit
groups, starting at the right-most bit and
replace each 4-bit group with the equivalent
hexadecimal symbol as in the following
example.
Convert the binary number to hexadecimal:
1100101001010111
Solution:
1100 1010 0101 0111

C A 5 7 = CA57
Hexadecimal-to-Decimal Conversion
One way to find the decimal
equivalent of a hexadecimal
number is to first convert the
hexadecimal number to binary and
then convert from binary to
decimal.
Convert the hexadecimal number 1C
to decimal:
1 C
0001 1100 = 16 +8+4 = 28
Convert the decimal number 650 to hexadecimal
by repeated division by 16.
650 = 40.625 0.625 x 16 = 10 = A (LSD)
16
40 = 2.5 0.5 x 16 = 8 = 8
16
2 = 0.125 0.125 x 16 = 2 = 2 (MSD)
16
The hexadecimal number is 28A
Binary-to-Octal Conversion
Conversion of a binary number to an octal number
is the reverse of the octal-to-binary conversion.
Convert the following binary numbers to octal:
110101
6 5 = 65

101111001
5 7 1 = 571
Decimal-to-Octal Conversion
Convert the decimal number 359 to octal. Each successive
division by 8 yields a remainder that becomes a digit in
the equivalent octal number. The first remainder
generated is the least significant digit (LSD).
359 = 44.875 0.875 x 8 = 7 (LSD)
8
44 = 5.5 0.5 x 8 = 4
8
5 = 0.625 0.625 x 8 = 5 (MSD)
8
The number is 547.
Octal-to-Binary Conversion
Octal Digit 0 1 2 3 4 5 6 7
Binary 000 001 010 011 100 101 110 111

Let’s convert the octal numbers 25 and 140.


2 5 1 4 0
010 101 001 100 000
Common Control Codes
• CR 0D carriage return
• LF 0A line feed
• HT 09 horizontal tab
• DEL 7F delete
• NULL 00 null

Hexadecimal code
Binary Math
Decimal Addition Example
1) Add 8 + 7 = 15
Add 3758 to 4657: Write down 5, carry 1

2) Add 5 + 5 + 1 = 11
111 Write down 1, carry 1
3758 3) Add 7 + 6 + 1 = 14
+ 4657 Write down 4, carry 1

8 415 4) Add 3 + 4 + 1 = 8
Write down 8
Decimal Addition Explanation
What just happened?
111
1 1 1 (carry)
3758 3 7 5 8
+4 6 5 7
8 14 11 15 (sum)

+ 4657 - 10 10 10 (subtract the base)


8 4 1 5

8415
So when the sum of a column is equal to or greater than the base, we
subtract the base from the sum, record the difference, and carry one to the
next column to the left.
Binary Addition Rules

Rules:
–0+0 =0
–0+1 =1
–1+0 =1 (just like in decimal)

 1+1 = 210
= 102 = 0 with 1 to carry

 1+1+1 = 310
= 112 = 1 with 1 to carry
Binary Addition Example 1
Col 1) Add 1 + 0 = 1
Example 1: Add Write 1
binary 110111 to 11100 Col 2) Add 1 + 0 = 1
Write 1
Col 3) Add 1 + 1 = 2 (10 in binary)
1 1 1 1 Write 0, carry 1
1 1 0 1 1 1 Col 4) Add 1+ 0 + 1 = 2
Write 0, carry 1
+ 0 1 1 1 0 0 Col 5) Add 1 + 1 + 1 = 3 (11 in binary)
Write 1, carry 1
1 0 1 0 0 1 1
Col 6) Add 1 + 1 + 0 = 2
Write 0, carry 1
Col 7) Bring down the carried 1
Write 1
Binary Addition Explanation
In the first two columns,
there were no carries.
What is actually
happened when we In column 3, we add 1 + 1 = 2
carried in binary? Since 2 is equal to the base, subtract
the base from the sum and carry 1.
1 1 1 1 In column 4, we also subtract
the base from the sum and carry 1.
1 1 0 1 1 1
In column 5, we also subtract
+ 0 1 1 1 0 0 the base from the sum and carry 1.
2 3 2 2 In column 6, we also subtract
- 2 2 2 2 . the base from the sum and carry 1.
In column 7, we just bring down the
1 0 1 0 0 1 1 carried 1
Binary Addition Verification

You can always check your Verification


answer by converting the 1101112  5510
figures to decimal, doing the +0111002 + 2810
addition, and comparing the 8310
answers.

64 32 16 8 4 2 1
1 0 1 0 0 1 1
1 1 0 1 1 1 = 64 + 16 + 2 +1
+ 0 1 1 1 0 0 = 8310

1 0 1 0 0 1 1
Binary Addition Example 2

Example 2: Verification
Add 1111 to 111010. 1110102  5810
+0011112 + 1510
7310

1 1 1 1 1
64 32 16 8 4 2 1
1 1 1 0 1 0 1 0 0 1 0 0 1
+ 0 0 1 1 1 1 = 64 + 8 +1
= 7310
1 0 0 1 0 0 1
Decimal Subtraction
Example

Subtract 1) Try to subtract 5 – 7  can’t.


4657 from 8025: Must borrow 10 from next column.
Add the borrowed 10 to the original 5.
Then subtract 15 – 7 = 8.
2) Try to subtract 1 – 5  can’t.
7 9 11 Must borrow 10 from next column.
But next column is 0, so must go to
8 10 2 15 column after next to borrow.
- 4 6 5 7 Add the borrowed 10 to the original 0.
Now you can borrow 10 from this column.
3 3 6 8
Add the borrowed 10 to the original 1..
Then subract 11 – 5 = 6
3) Subtract 9 – 6 = 3
4) Subtract 7 – 4 = 3
Decimal Subtraction Explanation

8 0 2 5
- 4 6 5 7

3 3 6 8
So when you cannot subtract, you borrow from the column to the left.
 The amount borrowed is 1 base unit, which in
decimal is 10.
 The 10 is added to the original column value, so you will
be able to subtract.
Binary Subtraction
Explanation

 In binary, the base unit is 2

 So when you cannot subtract, you borrow from the


column to the left.
 The amount borrowed is 2.
 The 2 is added to the original column value, so
you will be able to subtract.
Binary Subtraction Example 1
Col 1) Subtract 1 – 0 = 1
Example 1: Subtract Col 2) Subtract 1 – 0 = 1
Col 3) Try to subtract 0 – 1  can’t.
binary 11100 from 110011
Must borrow 2 from next column.
But next column is 0, so must go to
column after next to borrow.
2 1 Add the borrowed 2 to the 0 on the right.
0 0 2 2 Now you can borrow from this column
(leaving 1 remaining).
1 1 0 0 1 1 Add the borrowed 2 to the original 0.
Then subtract 2 – 1 = 1
- 1 1 1 0 0 Col 4) Subtract 1 – 1 = 0
subtract 0 – 1  can’t.
1 0 1 1 1 Col 5) Try toMust borrow from next column.
Add the borrowed 2 to the remaining 0.
Then subtract 2 – 1 = 1
Col 6) Remaining leading 0 can be ignored.
Binary Subtraction Verification

Verification
Subtract binary 1100112  5110
11100 from 110011:
- 111002 - 2810
2 1 2310
0 0 2 2
64 32 16 8 4 2 1
1 1 0 0 1 1 1 0 1 1 1
- 1 1 1 0 0 = 16 + 4 + 2 + 1
= 2310
1 0 1 1 1
Binary Subtraction
Example 2

Verification
Example 2: Subtract 1010012  4110
binary 10100 from 101001
- 101002 - 2010
2110

64 32 16 8 4 2 1
0 2 0 2
1 0 1 0 1
1 0 1 0 0 1 = 16 + 4 + 1
- 1 0 1 0 0 = 2110

1 0 1 0 1
Solve the following and show your complete solution.

1. (28 / 4) mod 3 =
2. 9 mod 7 =
3. 45 mod 9 =
4. 46 mod 5 =
Direction: compute for the sum and
difference of the following:
5. 101010 and 00011
6. 100111 and 10111
Parity Check
Even/odd parity
Computers can sometimes make errors when they
transmit data.
Even/odd parity:
is basic method for detecting if an odd number of bits has
been switched by accident.
Odd parity:
The number of 1-bit must add up to an odd number
Even parity:
The number of 1-bit must add up to an even number
Even/odd parity
The computer knows which parity it is using
If it uses an even parity:
If the number of 1-bit add up to an odd
number then it knows there was an error:
If it uses an odd:
If the number of 1-bit add up to an even
number then it knows there was an error:
However, If an even number of 1-bit is flipped the
parity will still be the same. But an error occurs
The even/parity can’t this detect this error:
Even/odd parity
It is useful when an odd number of 1-bit is flipped.

Suppose we have an 7-bit binary word (7-digits).


If you need to change the parity you need to add 1
(parity bit) to the binary word.
You now have 8 digit word.
However, the computer knows that the added bit is
a parity bit and therefore ignore it.
Example
Suppose you receive a binary bit word
“0101” and you know you are using
an odd parity.
Is the binary word errored?
The answer is yes:
There are 2 1-bit, which is an even number
We are using an odd parity
So there must have an error.
Parity Bit
• A single bit is appended to each data
chunk
– makes the number of 1 bits even/odd
• Example: even parity
– 1000000(1)
– 1111101(0)
– 1001001(1)
• Example: odd parity
– 1000000(0)
– 1111101(1)
– 1001001(0)
Parity Checking
Assume we are using even parity with 7-bit ASCII.
The letter V in 7-bit ASCII is encoded as 0110101.
How will the letter V be transmitted?
Because there are four 1s (an even number), parity is
set to zero.
This would be transmitted as: 01101010.
If we are using an odd parity:
The letter V will be transmitted as 01101011
Exercise 1
Suppose you are using an odd parity.
What should the binary word “1010”
look like after you add the parity bit?
Answer:
There is an even number of 1-bits.
So we need to add another 1-bit
Our new word will look like “10101”.
Exercise 2
Suppose you are using an even parity.
What should the binary word “1010”
look like after you add a parity bit?
Answer:
There is an even number of 1’s.
So we need to add another 0
Our new word will look like “10100”.
Repetition Code
If the source encoding is already done
and that the encoded message is of fix length
K, then the channel encoding by repetition is
performed by taking the bits, then repeat it
2r+1times, where r> 1 is fixed integer.
Example:
Source encoded message is 101 where k = 3.
Then choose 2, repeat 101, 5 times. The
encoded message is 101101101101101
Assume that the message is transmitted through a
noisy channel and distorted. The received message
is 111101100011100
the decoding process will be done in this manner.
Consider the position 1,4,7,10,13 of the received
message. Take the most frequent bit as the first
decoded bit
11010 = most frequent bit is 1
Then consider the position 2,5,8,11,14
10010 = most frequent bit is 0
Consider 3,6,9,12,15 positions
11101 = most frequent bit is 1
Then the decoded message is 101
111101100011100

001011000101100
Cryptography
Basic Terminology
• plaintext - the original message
• ciphertext - the coded message
• cipher - algorithm for transforming plaintext/ciphertext
• key - info used in cipher known only to sender/receiver
• encipher (encrypt) - converting plaintext to ciphertext
• decipher (decrypt) - recovering ciphertext from plaintext
• cryptography - study of encryption principles/methods
• cryptanalysis (code breaking) - the study of principles/
methods of deciphering ciphertext without knowing key
• cryptology - the field of both cryptography and cryptanalysis
What is a Caesar Cipher?
Caesar used to encrypt his messages using a very simple
algorithm, which could be easily decrypted if you know the
key.
He would take each letter of the alphabet and replace it with a
letter a certain distance away from that letter. When he got
to the end, he would wrap back around to the beginning.
Example with a shift of 3:

A B C D E F G H I J K L MN O P Q R S T U V WX Y Z
D E F G H I J K L MN O P Q R S T U V WX Y Z A B C
If you don’t Want to Make a Table
That’s fine! If you know what the shift is, you
can use something called modulo, which is
commonly shortened to mod.
Example to find 8 mod 5. We would divide 8
by 5 and find the remainder. So in this case,
8 mod 5 = 3.
In this case, 5 is called the modulus.
Can you solve these?
19 mod 5 2 mod 5 25 mod 5
4 2 0
When using a Caesar cipher, you assign each letter to an
index starting from 0.
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

You would then compute the following.

(plain letter index + key) mod (total number of letters)

This will give you the index of the encrypted letter!


As you can see, the modulus is the total number of letters
in the alphabet. For English, this modulus is 26.
Example
A 5 letter alphabet with only the letters A-E
First, assign each letter an index, starting from 0.
A B C D E
0 1 2 3 4

Then choose a key. For this example, use 2.


Try encoding the word BEAD using the formula for the previous
slide.
The index of the letter B is 1. The key is 2. The modulus is 5,
since the alphabet is 5 letters.
Use the algorithm: (1+2) = 3. 3 mod 5 = 3. The index of D is 3,
so B would become the letter D.
Using algorithm on each letter, can you encode the full word?
DBCA
What About Decoding?
To decode, you do the following:
(cipher letter index – key + total number of letters) mod
(total number of letters)
Does anyone know why we can’t just do:
(cipher letter index – key) mod (total number of letters)?
By adding the total number of letters, you don’t have
to end up taking the mod of a negative number.
See if you can decode DBCA to get back to the word BEAD.
Using ASCII for Caesar’s Cipher
• You probably noticed that the letter A is associated
with the number 65, B with 66, C with 67, and so
on.
• Most programming languages have a function that
can take a letter and find out it’s ASCII value.
• What would you need to do to make the ASCII
encoded letters usable for Caesar’s Cipher?
– Subtract 65 from each letter!
• Since it’s really simple to code for a computer to use
mod and assign the correct index for each letter,
you can probably see how programming a simple
encoder/decoder would be a cinch!
The Problem with Caesar’s Cipher
• It’s too easy to break! Let’s go back to our five letter alphabet.
• Say you had the word DBCA, but didn’t know the key. It would be easy to
make a list of all possible keys. If you expected the unencrypted text to be
in English, you could easily figure out which word was right:
Shift of 1 CABE
Shift of 2 BEAD  The clear winner!
Shift of 3 ADEC
Shift of 4 ECDB

• If you were using the full English alphabet, this would take longer, but for
someone trying to find our your secret, it’s a small price to pay!
How to decrypt a Caesar’s cipher without a
key
Example:
Given the code YQCMXQJ YQC
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Y Z A B C D E F G H 1 J K L M N O P Q R S T U V W X

A SEK – does not make sense


A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Q R S T U V W X Y Z A B C D E F G H I J K L M N O P

I AM WHAT I AM
Cryptography
• Originated from the Greek works KRYPTO
which means hidden and GRAPHENE means
writing.
• Is a method of making and breaking of secret
codes.
Cryptography
• Cryptology- is defined as the science of
making communication incomprehensible
to all people except those who have the
right to read and understand it.

The study of cryptology consists of two parts:


• • cryptography- which concerns itself with the
secrecy system itself and its design, and
• • cryptanalysis- which concerns itself with the
breaking of the secrecy system above.
DIFINITION:
Code
• a set of information that will allow
words to be changed to other words or
symbols that is not the type of analyze.
• The only way to decode a message is
by having the set of words and their
codes. If someone is able to get his
hands on the codebook, then every
secrecy message can be broken.
• Plaintext – the message that you wish
to put into a secret form. Plaintext is
usually written in all lower
• case letters without spaces. Numbers are
written out and punctuation is ignored. So
the message

“I will meet you at 5 PM in the mall” is written as:


iwillmeetyouatfivepminthemall
• Cipher - the method for altering the plain
• text.
• Ciphertext - the secret version of the
plaintext. So the plain text.
ex. iwillmeetyouatfivepminthemall
may be changed to:
• NBNQQRJJYDTZFYKNAJURNSYMJRFQQ
N BNQQ RJJY DTZ FY KNAJ UR NS YMJ RFQQ
• Encryption/Encipher - changing from plaintext
to ciphertext
• Decrytion/Decipher - changing from
ciphertext to plaintext
• Key - information that will allow someone to
encipher the plaintext and also decipher the
ciphertext.
The Additive (or shift) Cipher System

• The first type of monoalphabetic substitution cipher we


wish to examine is called the additive cipher.

• In this cipher method, each plaintext letter is replaced


by another character whose position in the alphabetis a
certain number of units away. We actually shift each
letter a certain number of places over.

• One of the first additive ciphers was used by Julius


Caesar around 50 B.C.
ILLUSTRATION:
(Message : Plain text ) Maths is fun
ILLUSTRATION:
(Message : Plain text ) Maths is fun

(Message : Coded Form) PDWKV LV IXQ


TERMS:
1. PLAIN TEXT = refers to the original message
2. CIPHERTEXT = refers to the coded message
3. KEY = refers to the strings of information
that is used to reveal the encrypted message
into readable form.
Simple Methods of Crytography
• Shift Cipher
- it is called Ceasar Cipher
- it is the simple type of substitution of cipher.
- It is the method of cryptology where in the
message is encrypted using a fixed number of
shift (move) in order the have the equivalent
character in a form of ciphertext.
- The number of shift can be any number from 0 -
25
Method of Shift Cipher
1. Shift to either left or right

Example: Shift of 3 to the right.

PROCEDURE:
- Place the sliding ruler to the first letter of the alphabet of the plain text to the
right using the indicated shift.
Method of Shift Cipher
1. Shift to either left or right

Example: Shift of 3 to the right.


Method of Shift Cipher
Example : Using a shift of 5 to the left.
Example :
• Using the key of shift of 3 to the right, encrypt
the Word “ CRYPTOGRAPHY”
Answer:
• CRYPTOGRAPHY = FUBSWRJUDKCT

Key:
Example
• Decrypt the cipher text, “BJY DN BJJY”, using
the key of 5-shifts to the left.
Answer :
• “BJY DN BJJY” = God is good.

Key:
Example:
3. Encrypt the word “ Our Lady of Fatima
University” using 2 shift to the left.
ANSWER:
Key:

“Our Lady of Fatima University”


MSP JYBW MD DYRGKY SLGTCPQGRW
EXAMPLE:
• Decryt the ciphertext “ G YK GLRCJJGECLR”
using 2-shift to the left.
ANSWER:
Key:

“ G YK GLRCJJGECLR”
I AM INTELLIGENT
BONUS POINTS: ( 10 –POINTS)
Encrypt the word “Phillipines”, using 6-shift to
the right.
Answer to the bonus point
Key:

PHILIPPINES
VNOROVVOTKS
BONUS POINTS: 10 PTS
Decrypt the word “ SGZNKSGZOIY”, using 2-shift
to the left.
Answer to the bonus point
Key:

SGZNKSGZOIY
MATHEMATICS
Method of Shift Cipher
• Using the Modulo operator
= The sender of the message uses the key K to
encrypt and to decrypt the secret message.
The key K may have any integer value from 0 –
25 and this will be shared to the person/s who
will be receiving the secret message.
Using the Modulo Operator
To encrypt the message:
• 1. Express the letters of the alphabet into an
integer from 0 to 25, that matches its order, for
example A = 0, B = 1, C = 3 ….., then label them
as C
• 2. Calculate Y = (C+ K) mod 26 , for every letter
of the message.
• 3. Convert the number Y into a letter following
the order of the letter of the alphabet.
M = 12 , W = 22, I = 8, S = 18, F = 5, U = 20, N = 13, T = 19, O = 14, L = 11,
E = 4, A = 0, R = 17

Example:
1. : Encrypt the message “MMW is fun to learn”
Let K = 5
Answer :

RRB NX KZS YT QJFWS


How to decrypt..
To Decrypt the Message:
1. Express the letters of the alphabet into an
integer from 0 to 25, that matches its order, for
example A = 0, B = 1, C = 3 ….., then label them
as Y
2. Calculate C = (Y - K) mod 26 , for every letter
of the decrypted message.
3. Convert the number C into a letter following
the order of the letter of the alphabet.
Example:
Decrypt the RRB NX KZS YT QJFWS
Example # 2
• Encrypt the word text “FATIMA” using the
Modulo Operator, given k = 20
Answer :

ZUNCGU

You might also like