Binary Codes Bs Psych 1 Y1 2
Binary Codes Bs Psych 1 Y1 2
Binary Code
So,
Bits form Bytes and Bytes form Words.
• 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
• 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
Introduction to Information
Technologies
95 Graphic codes
33 Control codes
Alphabetic codes
Numeric codes
Punctuation, etc.
“Hello, world” Example
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
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)
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
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
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
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.
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
• 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
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.
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
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:
“ 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 :
ZUNCGU