0% found this document useful (0 votes)
21 views71 pages

UNIT 1 Introduction

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)
21 views71 pages

UNIT 1 Introduction

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/ 71

TYPES OF COMPUTERS

By Size and Processing Power

1. Supercomputers: The fastest and most powerful computers, used for complex
scientific calculations.

2. Mainframes: Large, multi-user computers for bulk data processing and managing
large databases in big organizations.

3. Minicomputers: Mid-range multi-user computers, smaller and less powerful than


mainframes but more powerful than microcomputers.

4. Workstations: Powerful, single-user computers for specialized tasks like graphic


design and scientific research.

5. Microcomputers (Personal Computers): The most common type of computer for


individual use. This category includes desktops, laptops, tablets, and smartphones.
By Data Handling Capabilities
•Analog Computers: Process continuously changing data, such as temperature or pressure.
•Digital Computers: The most common type of computer; process data in discrete values using
binary digits (0s and 1s).
•Hybrid Computers: Combine features of both analog and digital computers for specialized
applications.

Other Specific Types


• Servers: Computers that provide data and services to other computers over a network.

• Embedded Computers: Specialized computers that are part of a larger device, designed to
perform a specific, dedicated task (e.g., in cars, appliances).
Functional Units of a Computer
• A computer system is made up of several functional units that work together to perform
tasks. These units can be categorized as follows:

1. Input Device

2. Output Device

3. Central Processing Unit (CPU)

4. Memory Unit

5. System Bus
1. Input Unit
•Function: Accepts data and instructions from the user or other computers.
It converts this information into a format that the computer can understand.
Examples of Devices: Keyboard, mouse, scanner, microphone.

2. Output Unit
•Function: Displays or presents the results of processing to the user. It converts the
computer's processed data into a human-readable form.
•Examples of Devices: Monitor, printer, speakers.
3. Central Processing Unit (CPU)
The CPU is considered the "brain" of the computer and is responsible for executing instructions and
performing calculations. It consists of three main sub-units:
•Arithmetic and Logic Unit (ALU):
•Function: Performs all arithmetic operations (like addition, subtraction, multiplication, and
division) and logical operations (like comparisons).
•Control Unit (CU):
•Function: Directs and coordinates all the operations of the computer system. It fetches
instructions from memory, decodes them, and sends control signals to other units to execute the
instructions. It acts as the "nerve center" of the computer.
•Registers:
•Function: Small, high-speed storage locations within the CPU that temporarily hold data,
instructions, and addresses that are currently being processed.
4. Memory Unit
•Function: Stores data and instructions that the computer needs for processing. It holds both
the program instructions and the data being manipulated.
•Types:
•Primary Memory (Main Memory): Temporary storage (e.g., RAM) that the CPU can
access directly and quickly. It holds the programs and data currently in use.
•Secondary Memory (Auxiliary Storage): Permanent storage (e.g., hard drives, SSDs,
USB drives) for long-term data and program storage.
5. System Bus
Function: A set of electrical pathways that connect all the functional units. It allows
data, addresses, and control signals to be transmitted between different components of
the computer.
INPUT AND OUTPUT DEVICES
I/O Devices

• Hardware that enables communication between a computer and


external entities (humans or other systems).
• Can both receive data (input) and deliver data (output).
• Acts as a bridge for seamless data exchange.
1. Input Devices
• Send signals to the CPU for processing.
• CPU processes input and sends signals to output devices.
I/O Devices

•Keyboard – Most common device for entering data; available in various key
configurations (84–108 keys).
•Mouse – Moves on-screen cursor; left click = select/move, right click = open menus.
•Joystick – Similar to a mouse; used in CAD and video games; moves in four directions.
•Light Pen – Pen-shaped device for selecting or drawing directly on the screen.
•Scanner – Converts paper documents/images into digital format for editing or storage.
•Bar Code Reader – Scans bar-coded data and converts it into alphanumeric values.
•Web Camera – Captures still images and videos; streams in real-time over the Internet.
Keyboard

Categories of Input Devices: • The most common input device for entering data.

Keyboard Devices • Layout similar to a typewriter, with additional


Pointing Devices
function keys.
Composite Devices
Game Controllers • Common sizes: 84 keys, 101/102 keys; modern
Visual Devices versions: 104 or 108 keys for Windows/Internet
Audio Input Devices functions.
Types of Keys

• Numeric Keys: It is used to enter numeric data or move


the cursor. It usually consists of a set of 17 keys.

• Typing Keys: The letter keys (A-Z) and number keys (09)
are among these keys.

• Control Keys: These keys control the pointer and the


screen. There are four directional arrow keys on it. Home,
End, Insert, Alternate(Alt), Delete, Control(Ctrl), etc., and
Escape are all control keys (Esc).

• Special Keys: Enter, Shift, Caps Lock, NumLk, Tab, etc.,


and Print Screen are among the special function keys on
the keyboard.

• Function Keys: The 12 keys from F1 to F12 are on the


topmost row of the keyboard.
MOUSE
• Most common pointing device.

• Used to move a cursor across the screen and


perform actions by clicking or dragging.

• Requires manual movement; does not move


automatically.

• Operates on a flat surface to control on-screen


coordinates and pointer movement.

• Left Button: Select or move items.

• Right Button: Opens additional menus.


3. Joystick

• Pointing device similar to a mouse.

• Spherical ball at top and bottom; lower


ball slides in a socket.

• Allows movement in four directions.

• Used in CAD applications and video


games.
LIGHT PEN

• Pointing device shaped like a pen.

• Used to select menu items or draw directly on


the monitor screen.

• Contains a photocell and optical system in a


small tube.

• When tip touches the screen with the button


pressed, it detects position and sends signal to
CPU.
Scanner

• Input device similar to a photocopier.

• Transfers information from paper to the


computer’s storage.

• Captures images and converts them


into digital format.

• Digital images can be edited before


printing.
Bar Code Reader

• Reads bar-coded data represented by


light and dark lines.

• Commonly used for product labelling,


book numbering, etc.

• Can be handheld or part of a stationary


scanner.

• Scans bar code, converts it to an


alphanumeric value, and sends it to the
computer.
Web Camera (Webcam)
• An input device that captures video
images of the scene in front of it.

• Can be built-in (e.g., in laptops) or USB-


connected.

• Small digital video camera used for taking


pictures and recording videos.

• Can stream real-time video over the


Internet with dedicated software.

• Supports HD video, but quality is usually


lower than standalone cameras.
Output Devices
Devices that display results after input is processed.
Output can be images, graphics, audio, video, etc.
Monitor (VDU)

• Primary output device of a computer.

• Creates images using tiny dots called pixels; more pixels = sharper
image.

Types of Monitors:

• CRT Monitor
• Uses pixels as visual elements.
• Higher resolution when pixels are smaller.

• Flat-Panel Display
• Lighter, thinner, uses less power than CRT.
• Can be wall-mounted or wearable.
• Used in calculators, video games, laptops, and graphical displays
Television

• Common output device for displaying


video and audio.

• Used for entertainment,


presentations, and media display.

• Modern TVs use plasma or flat-panel


displays instead of older CRT screens.
Printer
An output device that produces information on paper.

• Types of Printers:
• Impact Printers
• Character Printers – Print one character at a time.
• Line Printers – Print an entire line at once.
• Non-Impact Printers
• Laser Printers – High speed, high quality, uses laser
technology.
• Inkjet Printers – Sprays liquid ink directly onto
paper.
Both Input and Output Devices
• Touchscreen (Input + Output Device)

• Displays information like a monitor (output).

• Allows direct interaction by touching the screen


(input).

• Common in smartphones, tablets, ATMs,


kiosks.

• Replaces need for mouse/keyboard navigation.

• Advanced types (capacitive & resistive) can


detect multiple touches.
External Hard Drive (Input + Output Device)

• Stores (output) and retrieves (input) data.

• Connects via USB, Thunderbolt, or other


interfaces.

• Common uses:
• Backing up important data.
• Transferring large files between
computers.
• Expanding storage space.

• Portable – easily move files between systems.


Memory
Memory is the electronic storage space within a computer where data and
instructions are kept for immediate or future use.

It is a fundamental component that allows the computer's central processing unit


(CPU) to perform its tasks efficiently.
Primary Memory

• Volatile: Loses data when power is off.

• High Speed: Directly accessible by the CPU.

• Function: Holds data and instructions currently being processed.

Types:

• RAM: Main working memory for the operating system and applications.

• Cache: Ultra-fast, small memory for frequently used data, located near the CPU.

• Registers: Smallest, fastest storage inside the CPU itself.


Secondary Memory

• Non-Volatile: Retains data even without power.

• Lower Speed: Slower than primary memory; data must be loaded into RAM first.

• Function: Permanent, long-term storage of files and programs.

• Examples: Hard disk drives (HDDs), solid-state drives (SSDs), USB flash drives.

Key Distinction

• Primary memory is for immediate, temporary use by the CPU.

• Secondary memory is for permanent storage of data and applications.


Storage Devices
• A storage device is a piece of hardware that enables a computer to store and retrieve
data.

• It serves as a non-volatile repository for digital information, meaning it retains the data
even when the computer is turned off.

• Storage devices are crucial for saving the operating system, applications, and user files
for long-term use.
Feature Primary Storage Secondary Storage

To hold data and instructions that the CPU is actively To permanently store the operating system, applications, and
Purpose using for immediate processing. user files for long-term retention.
Volatile (mostly). Data is lost when the computer is Non-volatile. Data is retained even when the computer is
Volatility turned off. turned off.
Slower. Data must be transferred to primary storage first
Extremely Fast. The CPU can access data directly and before the CPU can access it. Access times are in
Speed quickly, typically in nanoseconds. milliseconds.
Limited Capacity. It is a smaller, more expensive type Large Capacity. It provides vast storage space at a lower cost
Capacity of memory. per gigabyte.
Cost Expensive per unit of storage (e.g., per gigabyte). Inexpensive per unit of storage.
RAM (Random Access Memory), Cache Memory, CPU Hard Disk Drives (HDDs), Solid-State Drives (SSDs), USB
Examples Registers. drives, CDs/DVDs.

Indirectly Accessible. The CPU must use input/output


Directly Accessible. The CPU can read from and write channels to transfer data to primary memory before it can be
CPU Access to primary memory without an intermediary. processed.

Essential for the computer to function. The computer Not strictly essential for the computer to function, but vital
Role in System cannot run without primary memory. for storing data and software.
Types of Storage Devices

• Hard Disk Drives (HDDs):

• Technology: Uses spinning magnetic platters and read/write heads to store data.

• Characteristics: Large storage capacity, low cost per gigabyte, but slower and more
prone to physical damage due to moving parts.

• Solid-State Drives (SSDs):

• Technology: Uses flash memory chips to store data electronically.

• Characteristics: Significantly faster than HDDs, more durable (no moving parts),
smaller, and lighter. They are generally more expensive per gigabyte.
Optical Storage Devices:
•Technology: Uses a laser to read and write data onto a disc's surface (pits and lands).
•Examples: CDs, DVDs, and Blu-ray discs.
•Characteristics: Removable and portable, often used for media distribution and backups.

Flash Memory Devices:


•Technology: Non-volatile memory that stores data in electronic memory cells.
•Examples: USB flash drives, SD cards, and microSD cards.
•Characteristics: Highly portable, durable, and convenient for transferring files between
devices.
Network-Attached Storage (NAS):
•Technology: A dedicated device with multiple hard drives that connects to a network.
•Characteristics: Provides a centralized, shared storage solution for multiple users or devices
in a home or business network.

Cloud Storage:
•Technology: A remote service that stores data on servers maintained by a third party.
•Characteristics: Accessible from any internet-connected device, scalable, and often used for
backups and file synchronization. It is a service rather than a physical device you own.
Number System Conversion

Number System Conversion Methods


A number N in base or radix b can be written as:
(N)b = dn-1 dn-2 -- -- -- -- d1 d0 . d-1 d-2 -- -- -- -- d-m
In the above, dn-1 to d0 is the integer part, then follows a radix point and then d-1 to d-

m is the fractional part.


dn-1 = Most significant bit (MSB)
d-m = Least significant bit (LSB)
1. Decimal to Binary Number System Conversion
For Integer Part: For example,
• Divide the decimal number by 2.
• Record the remainder (0 or 1).
• Continue dividing the quotient by 2 until the
quotient is 0.
• The binary equivalent is the remainders read
from bottom to top.
For Fractional Part:
For Integer Part (10):
• Multiply the fractional part by 2.
• Divide 10 by 2 → Quotient = 5, Remainder =
• Record the integer part (0 or 1). 0
• Take the fractional part of the result and • Divide 5 by 2 → Quotient = 2, Remainder = 1
repeat the multiplication.
• Divide 2 by 2 → Quotient = 1, Remainder = 0
• Continue until the fractional part becomes 0 or
reaches the desired precision. • Divide 1 by 2 → Quotient = 0, Remainder = 1
• The binary equivalent is the integer parts • Reading the remainders from bottom to top
recorded in sequence. gives 1010.
2. Binary to Decimal Number System Conversion
For Integer Part:
• Write down the binary number.
• Multiply each digit by 2 raised to the power of
its position, starting from 0 (rightmost digit).
• Add up the results of these multiplications.
Example: (1010.01)2
• The sum is the decimal equivalent of the
binary integer. 1x23 + 0x22 + 1x21+ 0x20 + 0x2 -1 + 1x2 -2 =
For Fractional Part: 8+0+2+0+0+0.25 = 10.25
• Write down the binary fraction. Thus, (1010.01)2 = (10.25)10
• Multiply each digit by 2 raised to the negative
power of its position, starting from -1 (first
digit after the decimal point).
• Add up the results of these multiplications.
• The sum is the decimal equivalent of the
binary fraction.
3. Decimal to Octal Number System Conversion
• For Integer Part: • Example: (10.25)10
• Divide the decimal number by 8. • For Integer Part (10):
• Record the remainder (0 to 7). • Divide 10 by 8 → Quotient = 1, Remainder = 2
• Continue dividing the quotient by 8 until the • Divide 1 by 8 → Quotient = 0, Remainder = 1
quotient is 0. • Octal equivalent = 12 (write the remainder,
• The octal equivalent is the remainders read read from bottom to top). So, the octal
from bottom to top. equivalent of the integer part 10 is 12.
• For Fractional Part: • For Fractional Part (0.25):
• Multiply the fractional part by 8. • Multiply 0.25 by 8 → Result = 2.0, Integer part
• Record the integer part (0 to 7). =2
• Take the fractional part of the result and • The fractional part ends here as the result is
repeat the multiplication. now 0. So, the octal equivalent of the fractional
part 0.25 is 0.2.
• Continue until the fractional part becomes 0
or reaches the desired precision. The octal equivalent of (10.25)10 = (12.2)8
• The octal equivalent is the integer parts
recorded in sequence.
4. Octal to Decimal Number System Conversion
• For Integer Part:
• Write down the octal number.
• Multiply each digit by 8 raised to the
power of its position, starting from 0
(rightmost digit).
• Add up the results of these multiplications. Example: (12.2)8
• The sum is the decimal equivalent of the 1 x 81 + 2 x 80 +2 x 8-1 = 8+2+0.25 = 10.25
octal integer. Thus, (12.2)8 = (10.25)10

• For Fractional Part:


• Write down the octal fraction.
• Multiply each digit by 8 raised to the
negative power of its position, starting
from -1 (first digit after the decimal point).
• Add up the results of these multiplications.
• The sum is the decimal equivalent of the
octal fraction.
5. Decimal to Hexadecimal Conversion
• For Integer Part: • Example: (10.25)10
• Divide the decimal number by 16. • Integer part:
• Record the remainder (0-9 or A-F). • 10 ÷ 16 = 0, Remainder = A (10 in decimal
• Continue dividing the quotient by 16 until is A in hexadecimal)
the quotient is 0. • Hexadecimal equivalent = A
• The hexadecimal equivalent is the • Fractional part:
remainders read from bottom to top. • 0.25 × 16 = 4, Integer part = 4
• For Fractional Part: • Hexadecimal equivalent = 0.4
• Multiply the fractional part by 16. • Thus, (10.25)10 = (A.4)16
• Record the integer part (0-9 or A-F).
• Take the fractional part of the result and
repeat the multiplication.
• Continue until the fractional part becomes
0 or reaches the desired precision.
• The hexadecimal equivalent is the integer
parts recorded in sequence.
6. Hexadecimal to Decimal Conversion
• For Integer Part:
• Write down the hexadecimal number.
• Multiply each digit by 16 raised to the power
of its position, starting from 0 (rightmost
digit).
• Add up the results of these multiplications.
• The sum is the decimal equivalent of the Example: (A.4)16
hexadecimal integer. (A × 160) + (4 × 16-1) = (10 × 1) + (4 × 0.0625)
• For Fractional Part: Thus, (A.4)16 = (10.25)10
• Write down the hexadecimal fraction.
• Multiply each digit by 16 raised to the
negative power of its position, starting from -1
(first digit after the decimal point).
• Add up the results of these multiplications.
• The sum is the decimal equivalent of the
hexadecimal fraction.
7. Hexadecimal to Binary Number System Conversion

• To convert from Hexadecimal to Binary:

• Each hexadecimal digit (0-9 and A-F) is represented by a 4-bit


binary number.
Example: (3A)16
• (3)16 = (0011)2
• (A)16 = (1010)2
• Thus, (3A)16 = (00111010)2

• For each digit in the hexadecimal number, find its corresponding


4-bit binary equivalent and write them down sequentially.
8. Binary to Hexadecimal Number System Conversion

• To convert from Binary to Hexadecimal:

• Start from the rightmost bit and divide the


binary number into groups of 4 bits each. Example: (1111011011)2
0011 1101 1011
• If the number of bits isn't a multiple of 4, pad
|||
the leftmost group with leading zeros.
3DB
• Each 4-bit binary group corresponds to a
Thus, (001111011011 )2 = (3DB)16
single hexadecimal digit.

• Replace each 4-bit binary group with the


corresponding hexadecimal digit.
9. Binary to Octal Number System
Octal Binary Equivalent
0 000
• To convert from binary to octal:
1 001
• Starting from the rightmost bit, divide the 2 010

binary number into groups of 3 bits. 3 011


4 100
• If the number of bits is not a multiple of 3, 5 101
add leading zeros to the leftmost group. 6 110

Replace each 3-bit binary


• Each 3-bit binary group corresponds to a group with the corresponding
octal digit.
single octal digit. Example: (111101101)2 111
111 101 101
• The binary-to-octal conversion for each 3-bit | | |
7 5 5
group is as follows:
Thus, (111101101)2 =
(755)87
10. Octal to Binary Number System Conversion

Example: (153)8
• To convert from octal to binary:
• Break the octal number into digits: 1, 5, 3
• Each octal digit (0-7) corresponds to a 3-bit
• Convert each digit to binary:
binary number.
• 1 in octal = 001 in binary
• For each octal digit, replace it with its
• 5 in octal = 101 in binary
corresponding 3-bit binary equivalent. • 3 in octal = 011 in binary

• Thus, (153)8 = (001101011)2


Computer Codes
• Computer code, or simply "code," is a set of instructions written in a programming
language that tells a computer what to do.

• It is the fundamental building block of all software, from operating systems and mobile
apps to websites and video games.
1. The Purpose of Code
•Communication: Code acts as a translator between humans and computers. It allows a
programmer to express logical commands in a way that a computer can understand and
execute.
•Instruction: Code is a detailed, step-by-step set of instructions that a computer follows to
perform a specific task. This can be as simple as adding two numbers or as complex as
rendering a 3D environment.
•Problem-Solving: At its core, writing code is about solving problems. Programmers break down
a complex problem into smaller, manageable steps, and then write code to solve each of those
steps.
2. Types of Code
Computer code exists in different forms, each with a specific purpose:
Source Code: This is the human-readable code written by a programmer using a programming
language like Python, Java, or C++. It is the original, raw code that can be viewed and edited.
Machine Code: This is the low-level binary code (0s and 1s) that a computer's central processing
unit (CPU) can directly execute. It is the only language a computer truly understands.
Object Code: This is the output of a compiler, which translates source code into a form that is closer
to machine code. It's an intermediate step in the process of turning human-readable code into an
executable program.
3. How Code Works
The process of turning code into a functioning program involves several steps:
•Writing: A programmer writes source code using a programming language, which has its
specific syntax and rules.
•Translating: Since a computer only understands machine code, the source code must be
translated. This is done by a compiler or an interpreter.
•A compiler translates the entire source code into machine code at once, creating an
executable file.
•An interpreter translates and executes the code line by line, without creating a separate
executable file.
•Executing: The machine code is then executed by the computer's CPU, which performs the
instructed tasks.
4. Code and Programming Languages
Code is always written in a specific programming language. There are thousands of
programming languages, each with its own strengths and use cases.

•High-level languages (like Python, JavaScript, and C++) are designed to be easier for
humans to read and write. They use English-like syntax and have a higher level of abstraction
from the computer's hardware.

•Low-level languages (like Assembly) are closer to machine code and give a programmer
more direct control over the computer's hardware, but are much more difficult to use.
Binary Subtraction and Subtraction
• Binary addition and subtraction are fundamental arithmetic
operations for computers, which operate in the binary number
system (base-2).

• They follow a set of simple rules that are essential for understanding
how computers perform calculations.
Binary Addition
Binary addition is similar to decimal addition, Step-by-step:
but with a base of 2. When the sum of two bits
• Rightmost column (1s place): 1+1=10_2. Write
is greater than 1, a "carry" is generated and down 0 and carry over 1.
moved to the next higher position.
• Next column (2s place): 1+1+1 (the carry) =11_2.
Rules of Binary Addition Write down 1 and carry over 1.
•0+0=0
• Next column (4s place): 0+1+1 (the carry) =10_2.
•0+1=1 Write down 0 and carry over 1.
•1+0=1
• Leftmost column (8s place): 1+1 (the carry)
•1+1=10_2 (This is 0 with a carry of 1) =10_2. Write down 10.
•1+1+1=11_2 (This is 1 with a carry of 1)
• The result is 10010_2. (Decimal equivalent:
Example: Add 1011_2 and 111_2
11+7=18)
1 1 1 (Carry) 1 0 1 1 + 1 1 1 --------- 1 0 0 1 0
Binary Subtraction
Binary subtraction also follows rules similar to decimal Step-by-step:
subtraction, using a "borrow" when a larger digit is
• Rightmost column (1s place): 0−1. You can't do
subtracted from a smaller one.
this directly, so you "borrow" a 1 from the next
• Rules of Binary Subtraction
column. The 1 in the next column becomes a 0.
• 0−0=0 The 0 in the current column becomes a 10_2
• 1−0=1 (which is 2 in decimal). Now, 10_2−1=1. Write

• 1−1=0 down 1.

• 0−1=1 (with a borrow of 1) • Next column (2s place): The digit is now 0 (due
When you "borrow" from the next column, that column's to the borrow). 0−0=0. Write down 0.
digit is reduced by 1. A borrowed '1' from the next
• Leftmost column (4s place): 1−1=0. Write down
column is equivalent to '2' in the current column (since
the base is 2). Example: Subtract 101_2 from 110_2 0.
0 10 (Borrow) 1 1 0 - 1 0 1 --------- 0 0 1 • The result is 001_2, or simply 1_2. (Decimal
equivalent: 6−5=1)
Subtraction with Two's Complement
• For more complex subtractions, especially when • Find the one's complement of the subtrahend by
flipping all the bits (0s become 1s, and 1s become
dealing with signed numbers, computers often use a
0s).
method called two's complement.
• Add 1 to the one's complement to get the two's
• This method converts the subtraction problem into complement.

an addition problem, which is simpler for digital • Add the two's complement of the subtrahend to the
minuend.
circuits to handle.
• If a carry-out is produced from the leftmost bit,
• To subtract a number (the subtrahend) from discard it. The remaining bits are the answer. If
another (the minuend) using two's complement: there is no carry-out, the result is negative and in
two's complement form; you need to take the two's
complement of the result and add a negative sign.
1's and 2's complement of a Binary Number
1's and 2’s Complement of a Binary Number
• Given a binary number s represented as a string. The task is to return its 1's
complement and 2's complement in the form of an array as [one’s Complement, two’s
Complement].

• The 1's complement of a binary number is obtained by flipping all its bits. 0 becomes 1,
and 1 becomes 0. Positive numbers remain unchanged, whereas negative numbers are
represented by taking the 1's complement of their positive counterparts.

• For example, in 8-bit notation:

+9 is represented as 00001001.

-9 is represented as 11110110, which is the 1's complement of 00001001.


Examples:
Input: s = "0111"
Output: 1000
Explanation: Each bit is flipped, i.e. 0 becomes 1, and 1 becomes 0.
Input: s= "1100"
Output: 0011
Explanation: Each bit is flipped, i.e. 0 becomes 1, and 1 becomes 0.
• The 2's complement of a binary number is obtained by finding the 1's
complement (flipping all bits) and then adding 1 to the result.

• In 2's complement representation, the Most Significant Bit (MSB) represents the sign.

• A 0 indicates a positive number, while a 1 indicates a negative number. The remaining


bits represent the magnitude.

• Positive numbers are represented the same way as in 1's complement and sign-
bit representation.

• Negative numbers are obtained by taking the 2's complement of their positive
counterparts.
Examples:
Input: s = "0111"
Output: 1001
Explanation: Find 1's complement -> 1000, then add 1 -> 1000 + 1 = 1001
Input: "1100"
Output: 0100
Explanation: Find 1's complement -> 0011, then add 1 -> 0011 + 1 = 0100

You might also like