0% found this document useful (0 votes)
15 views14 pages

Introduction To Computer Science

The document provides a comprehensive overview of computer science fundamentals, focusing on number systems including binary, decimal, octal, and hexadecimal. It explains the principles of binary counting, conversions between different number systems, and the significance of ASCII and BCD in representing data. Additionally, it covers the advantages of binary code in digital systems and the methods for converting between various numerical representations.

Uploaded by

cj686528
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views14 pages

Introduction To Computer Science

The document provides a comprehensive overview of computer science fundamentals, focusing on number systems including binary, decimal, octal, and hexadecimal. It explains the principles of binary counting, conversions between different number systems, and the significance of ASCII and BCD in representing data. Additionally, it covers the advantages of binary code in digital systems and the methods for converting between various numerical representations.

Uploaded by

cj686528
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Introduction to Computer Science:

Computer Science is the study of computers, their components, programming, and applications.
It involves understanding how computers work, how to give instructions (programming), and how
they solve problems efficiently.

Number System

A Number System is a method of representing numbers using a set of symbols (digits)


according to certain rules.

Radix (Base)

 The radix or base of a number system is the total number of unique digits, including
zero, used in that system.

Binary Number System – Definition

The Binary Number System is a method of representing numbers using only two symbols:
0 and 1.
It has a base of 2, meaning each position (bit) represents a power of 2.
This system is used in computers because digital circuits recognize two states: ON (1) and
OFF (0).

Advantages of Binary Code

1. Simplicity of Design
o Binary uses only two symbols: 0 and 1.
o Easy to represent using electrical signals (ON = 1, OFF = 0).
2. Reliability in Digital Systems
o Less chance of error because only two states are used.
o Works well in noisy environments.
3. Compatibility with Logic Gates
o Computers use logic gates (AND, OR, NOT) which work naturally with
binary values.
4. Efficient Data Processing
o All types of data (numbers, text, images, audio) can be converted into binary.
5. Storage and Memory Optimization
o Binary makes it simple to store and retrieve data in electronic devices.

Radix (Base) – Definition

The radix (or base) of a number system is the total number of unique digits, including zero,
used to represent numbers in that system.
Explanation

 Determines how many symbols are used in a number system.


 Positional value of digits is calculated using powers of the radix

Examples

1. Binary System – Radix 2


o Digits: 0, 1
2. Decimal System – Radix 10
o Digits: 0 to 9
3. Octal System – Radix 8
o Digits: 0 to 7
4. Hexadecimal System – Radix 16
o Digits: 0–9 and A–F

What is Binary Counting?

 Binary uses only 0 and 1 to count numbers.


 It is like decimal counting, but instead of 10 digits (0–9), we only have 2 digits (0 and
1).
 When we reach 1, the next number becomes 10 (just like after 9 in decimal, we write
10).

How Counting Works

 Decimal counting: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11...


 Binary counting: 0, 1, 10, 11, 100, 101, 110, 111, 1000...

1.2 Binary to Decimal Conversion

Definition

Binary to decimal conversion is the process of changing a number from the binary system
(base 2) to the decimal system (base 10).

Method

1. Write down the binary number.


2. Multiply each digit by 2ⁿ
where n is the position of the digit from the right (starting from 0).
3. Add all the results to get the decimal number.

For Fractional Part

1. Start from the first digit after the decimal point.


2. Assign negative powers of 2 (2^−1,2^−2,2^−3,…2^{-1}, 2^{-2}, 2^{-3},..)
3. Multiply each binary digit by its power of 2.
4. Add them to get the fractional decimal value.

1. Binary → Octal (Base 2 → Base 8)

Since 8=238 = 2^38=23, we group binary digits into 3 bits.

Integer Part

1. Group binary digits into 3 bits from the right.


2. Add leading zeros if necessary to complete the last group.
3. Replace each 3-bit group with its octal equivalent.

Fractional Part

1. Group binary digits into 3 bits from the left (after the binary point).
2. Add trailing zeros if necessary to complete the last group.
3. Replace each 3-bit group with its octal equivalent.

2. Binary → Hexadecimal (Base 2 → Base 16)

Since 16=2416 = 2^416=24, we group binary digits into 4 bits.

Integer Part

1. Group binary digits into 4 bits from the right.


2. Add leading zeros if necessary to complete the last group.
3. Replace each 4-bit group with its hexadecimal equivalent (0–9, A–F).

Fractional Part

1. Group binary digits into 4 bits from the left (after the binary point).
2. Add trailing zeros if necessary to complete the last group.
3. Replace each 4-bit group with its hexadecimal equivalent (0–9, A–F).
Decimal Number System (Base 10)

Definition

The decimal number system is the standard system we use in daily life. It has base (radix)
10 and uses ten digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.

Features

 Each digit’s value depends on its position (place value).


 Position values are powers of 10 (… 10³, 10², 10¹, 10⁰).
 Widely used in arithmetic, finance, and everyday calculations

Steps for Decimal → Binary Conversion

For Integer Part

1. Divide the decimal number by 2.


2. Write down the remainder (0 or 1).
3. Repeat the process with the quotient until it becomes 0.
4. Read the remainders from bottom to top → gives binary number.

For Fractional Part

1. Multiply the fractional part by 2.


2. Record the integer part (0 or 1).
3. Repeat the process with the fractional result until it becomes 0 (or desired accuracy).
4. Read the digits from top to down → gives binary fraction.

Decimal → Octal (Steps)

Integer Part

1. Divide the decimal number by 8.


2. Write the remainder.
3. Repeat until quotient = 0.
4. Read remainders bottom → top.

Fractional Part

1. Multiply the fraction by 8.


2. Write the integer part.
3. Repeat with the new fraction.
4. Read digits top → down.
Decimal → Hexadecimal (Steps)

Integer Part

1. Divide the decimal number by 16.


2. Write the remainder (0–9, A–F).
3. Repeat until quotient = 0.
4. Read remainders bottom → top.

Fractional Part

1. Multiply the fraction by 16.


2. Write the integer part (0–9, A–F).
3. Repeat with the new fraction.
4. Read digits top → down.

Hexadecimal Number System (Base 16)

Hexadecimal Number System

 The Hexadecimal Number System is a positional number system with base 16.
 It uses 16 symbols:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
o Here:
 A = 10, B = 11, C = 12, D = 13, E = 14, F = 15 (in decimal).

Place Values

 Each position in a hexadecimal number represents a power of 16.


o Rightmost digit → 160
o Next digit → 161
o Next digit → 162, and so on.

Relation with Binary

 1 hexadecimal digit = 4 binary bits (nibble)


 Example:
o A (hex) = 1010 (binary)
o F (hex) = 1111 (binary)

Uses of Hexadecimal

 Short and compact representation of long binary numbers.


 Widely used in:
o Programming and debugging
o Computer memory addressing
o Color codes in HTML/CSS (e.g., #FF5733)

Hexadecimal to Binary Conversion

Integer Part

1. Write down the hexadecimal number.


2. Replace each hex digit with its 4-bit binary equivalent.
3. Arrange the groups in the same order (left to right).
4. Remove any leading zeros (if needed).

Fractional Part

1. Write down the hexadecimal fraction.


2. Replace each hex digit after the decimal point with its 4-bit binary equivalent.
3. Arrange the groups in the same order (after the binary point).
4. Do not drop trailing zeros unless simplification is required.

Hexadecimal to Decimal Conversion

Integer Part

1. Write the hexadecimal number.


2. Starting from the rightmost digit, multiply each digit by
16position16^{position}16position, where position = 0,1,2,…
3. Add all the products together.
4. The result is the decimal value.

Fractional Part

1. Write the hexadecimal fraction.


2. Starting from the first digit after the decimal point, multiply each digit by
16−position16^{-position}16−position, where position = 1,2,3,…
3. Add all the products together.
4. The result is the decimal fractional value.

Final Decimal Number = (Sum of Integer Part) + (Sum of Fractional Part)

Hexadecimal to Octal Conversion

Since hexadecimal (base 16) and octal (base 8) are not direct multiples, we usually convert
via binary as an intermediate step.
Integer Part

1. Write the hexadecimal number.


2. Convert each hex digit into its 4-bit binary equivalent.
3. Group the binary digits into 3-bit groups (from right to left).
4. Replace each 3-bit group with its octal equivalent.
5. Write the octal digits in the same order (remove any leading zeros).

Fractional Part

1. Write the hexadecimal fraction.


2. Convert each hex digit into its 4-bit binary equivalent.
3. Group the binary digits into 3-bit groups (from left to right after the binary point).
4. Replace each 3-bit group with its octal equivalent.
5. Write the octal digits in the same order (remove any trailing zeros if needed).

Rule Summary:
Hex → Binary (4 bits each) → Group into 3 bits → Octal.

Octal Number System

 The Octal Number System is a positional number system with base 8.


 It uses 8 digits only:
0, 1, 2, 3, 4, 5, 6, 7

Place Values

 Each position in an octal number represents a power of 8.


o Rightmost digit → 808^080
o Next digit → 818^181
o Next digit → 828^282, and so on.

Relation with Binary

 1 octal digit = 3 binary bits


o Example:
 (7)₈ = (111)₂
 (5)₈ = (101)₂
Uses of Octal

 Short representation of long binary numbers.


 Used in:
o Computer systems (especially older ones).
o Digital electronics.
o File permissions in UNIX/Linux (e.g., chmod 755).

1. Octal → Binary

Since 8=238 = 2^38=23, each octal digit is directly replaced by 3 binary bits.

Integer Part

1. Write the octal number.


2. Replace each octal digit with its 3-bit binary equivalent.
3. Arrange groups in the same order (remove extra leading zeros if needed).

Fractional Part

1. Write the octal fraction.


2. Replace each octal digit (after the decimal) with its 3-bit binary equivalent.
3. Arrange groups in the same order (remove trailing zeros if needed).

2. Octal → Decimal

Integer Part

1. Write the octal number.


2. Starting from the rightmost digit, multiply each digit by
8position8^{position}8position, where position = 0,1,2,…
3. Add all the products.

Fractional Part

1. Write the octal fraction.


2. Starting from the first digit after the decimal point, multiply each digit by
8−position8^{-position}8−position, where position = 1,2,3,…
3. Add all the products.

👉 Final Decimal Number = (Integer part sum) + (Fractional part sum)


3. Octal → Hexadecimal

Since octal (base 8) and hexadecimal (base 16) are not direct multiples, we convert via
binary.

Integer Part

1. Convert the octal number → binary (3 bits per digit).


2. Group binary digits into 4 bits from the right.
3. Replace each 4-bit group with its hex equivalent (0–9, A–F).

Fractional Part

1. Convert the octal fraction → binary (3 bits per digit).


2. Group binary digits into 4 bits from the left (after decimal).
3. Replace each 4-bit group with its hex equivalent (0–9, A–F).

Binary Coded Decimal (BCD)

Definition

 BCD (Binary Coded Decimal) is a number system where each decimal digit (0–9)
is represented by its 4-bit binary equivalent.
 It is not a separate base system like binary, octal, or hex, but a way of encoding
decimal numbers in binary form.

BCD Representation

 Each decimal digit (0–9) is written in binary (4 bits).


 Example mapping:
o 0 → 0000
o 1 → 0001
o 2 → 0010
o 3 → 0011
o 4 → 0100
o 5 → 0101
o 6 → 0110
o 7 → 0111
o 8 → 1000
o 9 → 1001

Only 0000 to 1001 are valid BCD codes (1010–1111 are unused).
ASCII (American Standard Code for Information Interchange)

1. What is ASCII?

 ASCII is a code used by computers to represent text characters (letters, numbers,


symbols, control keys) in binary form.
 Every character (like A, 5, @, space, etc.) is given a unique number.
 That number is then stored inside the computer in binary (0s and 1s).

2. How many characters?

 ASCII uses 7 bits → this gives 128 codes (from 0 to 127 in decimal).
 Later, an 8-bit extended ASCII was introduced (0–255) for more symbols.

3. ASCII Code Groups

 0–31 → Control characters (not printed, used for actions like Enter, Tab, Bell).
 32–126 → Printable characters (letters, numbers, punctuation, space).
 127 → Delete (DEL).

4. Examples

Character Decimal Code Binary (7-bit)

Space 32 0100000

0 48 0110000

A 65 1000001

a 97 1100001

! 33 0100001

So, when you type A, the computer actually stores 1000001 (binary for 65).

5. Why is ASCII important?

 It became the first standard way to exchange text between computers.


 Ensures that the same binary code means the same character on every machine.
 Used in early programming, data transfer, and internet protocols.
 Basis for modern character sets (Unicode is built on ASCII).

1’s-complement and 2’s-complement subtraction — detailed rules +


examples

Preliminaries / rules used in examples

 Use a fixed n-bit width for both numbers (all examples below use 5 bits) — you
must use the same width for minuend and subtrahend.
 Always write both numbers in that n-bit form before starting (add leading zeros if
needed).
 Decimal checks are shown to verify correctness.

A. 1’s-complement subtraction

Rule (integer subtraction A − B):

1. Represent A and B in n bits.


2. Find 1’s complement of B (flip every bit: 0→1, 1→0).
3. Add that 1’s-complement of B to A (binary addition).
4. If there is a carry out from the MSB (an extra leftmost 1), add that carry (1) to the
LSB of the result — this is called end-around carry. The final result (after adding
end-around carry) is the correct positive difference.
5. If there is no carry out, the result is negative. To get its magnitude: take the 1’s
complement of the sum and put a negative sign.

Example 1 (positive result)

Compute: 101012−00111210101_2 - 00111_2101012−001112 (5-bit)

 Decimal check: 21 − 7 = 14.

Steps:

1. A = 10101 (21). B = 00111 (7).


2. 1’s complement of B = flip 00111 → 11000.
3. Add:

10101
+ 11000
=1 01101 ← there is a carry out (leftmost 1)

4. Drop the leftmost carry and perform end-around carry: result without carry = 01101.
Add the carry 1 to LSB:
01101
+ 00001
= 01110

5. 01110 = 14 → matches 21 − 7 = 14. ✅

Example 2 (negative result)

Compute: 001112−10101200111_2 - 10101_2001112−101012 (5-bit)

 Decimal: 7 − 21 = −14

Steps:

1. A = 00111 (7). B = 10101 (21).


2. 1’s complement of B = flip 10101 → 01010.
3. Add:

00111
+ 01010
= 10001 ← no carry out

4. No carry → result is negative. Take 1’s complement of 10001:

 1’s complement of 10001 → 01110 (decimal 14).

5. Answer = −01110 → −14. ✅

Notes (1’s-complement):

 Two zeros exist (all zeros = +0, all ones = −0) in pure 1’s complement systems —
awkward, which is why 2’s complement is preferred in practice.
 Use end-around carry only when there is a carry out.

B. 2’s-complement subtraction

Rule (integer subtraction A − B):

1. Represent A and B in n bits.


2. Find 2’s complement of B: invert bits (1’s complement) then add 1. This gives the
binary for (−B) in 2’s-complement arithmetic (within the chosen width).
3. Add A and 2’s complement of B.
4. If there is a carry out (an extra leftmost 1), discard that carry — the remaining n bits
are the correct positive result.
5. If there is no carry out, the n-bit result is negative in 2’s-complement form. To find
magnitude, take the 2’s complement of that result and add a negative sign.

Why 2’s complement is preferred: unique zero, simpler hardware (no end-around carry),
straightforward sign representation.
Example 3 (positive result) — same numbers as earlier

Compute: 101012−00111210101_2 - 00111_2101012−001112 (5-bit)

 Decimal check: 21 − 7 = 14.

Steps:

1. A = 10101. B = 00111.
2. 2’s complement of B:
o invert 00111 → 11000
o add 1 → 11001 (this is 2’s complement of 7)
3. Add:

10101
+ 11001
=1 01110 ← carry out = 1

4. Discard carry out → result 01110 (14). ✅

Example 4 (negative result)

Compute: 001112−10101200111_2 - 10101_2001112−101012 (5-bit)

 Decimal: 7 − 21 = −14

Steps:

1. A = 00111. B = 10101.
2. 2’s complement of B:
o invert 10101 → 01010
o add 1 → 01011
3. Add:

00111
+ 01011
= 10010 ← no carry out

4. No carry out → result 10010 is negative (2’s complement form). To get magnitude:
o take 2’s complement of 10010: invert → 01101, add 1 → 01110 (14).
5. Answer = −01110 = −14. ✅

Notes (2’s-complement):

 Single representation of zero (good).


 Hardware uses same adder for addition and subtraction (just invert and add 1 for
subtrahend).
 If working in fixed n bits, be mindful of overflow: if the true mathematical result is
outside representable range for n bits, you get overflow. For signed numbers,
overflow detection rules differ (but for classroom problems with small numbers,
choose n large enough).

Quick summary — HOW TO DO subtraction using complements (cheat-


sheet)

1’s complement method (n bits):

1. 1’s-complement(B) → add to A.
2. If carry out → add 1 (end-around carry) → positive result.
3. If no carry → 1’s-complement(sum) and mark negative.

2’s complement method (n bits):

1. 2’s-complement(B) = invert(B) + 1 → add to A.


2. If carry out → discard it → positive result.
3. If no carry → result is negative in 2’s-complement; take 2’s-complement to get
magnitude and put minus sign.

You might also like