SlideShare a Scribd company logo
1 of 27
https://softuni.org
Processing Bits in the Integer Numbers
Bitwise Operations in Programming
Svetlin Nakov, PhD
Co-Founder, Innovation and
Inspiration Manager at SoftUni
https://nakov.com
sli.do
#nakov
Have a Question?
2
3
 The 4 groups of software development skills
 Coding, algorithms, development concepts, technologies
 Numerals systems
 Decimal, binary, hexadecimal
 Conversion between numeral systems
 Bitwise operations (&, I, ^, ~)
 Processing bits in programming
 Reading / writing bits from integers
 How to become a software developer?
Agenda
 Coding skills – 20%
 Algorithmic thinking – 30%
 Fundamental concepts of the
software developer profession – 25%
 Programming languages &
software technologies – 25%
Skills of Software Developers
4
0|1
Bits
What is a Bit?
 Bit == the smallest unit of data used in computing
 Takes only one of two values: either a 0 or 1
 1 bit can store anything with two separate states
 Logical values (true / false)
 Algebraic signs (+ / -)
 Activation states (on / off)
 Bits are organized in computer memory in
sequences of 8 bits, called bytes (octets)
Bit
6
 Bit – single 0 or 1, representing a bit of data
 Byte (octet) == 8 bits == the smallest addressable unit
in the computer memory
 KB (kilobyte) == 1024 bytes (sometimes 1000 bytes)
 MB (megabyte) == 1024 KB == 1048576 bytes
 GB (gigabyte) == 1024 MB == 1073741824 bytes
 TB (terabyte) == 1024 GB == 1099511627776 bytes
 PB (petabyte) == 1024 TB == 1125899906842624 bytes
Bit, Byte, KB, MB, GB, TB, PB
7
5
101b
0x8
Numerals Systems
Decimal, Binary and Hexadecimal
 Numeral system == system for representing
numbers in written form using sequence of digits
 Positional numeral systems == the value of each
digit depends on its position
 These numeral systems has a base (e.g. 2, 10, 16)
Numeral Systems
Decimal
(base = 10)
Binary
(base = 2)
Hexadecimal
(base = 16)
30 111110 1E
45 101101 2D
60 111100 3C
9
Decimal Numbers
 Decimal numbers (base 10)
 Represented using 10 numerals (called digits):
 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
 Each position represents a power of 10
401 = 4*102 + 0*101 + 1*100 =
= 4*100 + 0*10 + 1*1 =
= 400 + 0 + 1 = 401
10
Binary Numbers
 The binary system is used in computer systems
 Binary numbers (base 2)
 Represented by sequence of 0 or 1
 Each position represents a power of 2
5 -> 101b
101b = 1*22 + 0*21 + 1*20 = 4 + 0 + 1 = 5
11
1010b = 1*23 + 0*22 + 1*21 + 0*20 =
8 + 0 + 2 + 0 = 10
Binary and Decimal Conversion
 Binary to decimal
 Multiply each digit to its
magnitude (power of 2)
 Decimal to binary
 Divide to the base (2) until
0 is reached and take the
reminders in reversed order
1011b = 1*23 + 0*22 + 1*21 + 1*20 =
= 1*8 + 0*4 + 1*2 + 1*1 =
= 8 + 0 + 2 + 1 =
= 11
11 / 2 = 5 (1) // last digit
5 / 2 = 2 (1) // previous digit
2 / 2 = 1 (0) // previous digit
1 / 2 = 0 (1) // fist digit
Result: 1011
12
>>
Bitwise Operations
Bitwise Operators and Bit Shifts
Bitwise Operators
 Bitwise operators work with the binary representations of
the numbers, applying bit by bit calculations
 The operator ~ turns all 0 to 1 and all 1 to 0 (like ! for
boolean expressions but bit by bit)
 The operators |, & and ^ behave like ||, && and ^
for boolean expressions but bit by bit
Operator | | | & & & ^ ^ ^
Operand1 0 1 1 0 1 1 0 0 1
Operand2 0 0 1 0 0 1 0 1 1
Result 0 1 1 0 0 1 0 1 0
14
 Bitwise NOT (~)
 Bitwise AND (&)
Bitwise Operators – Examples
5 // 0101
~5 // 1010
5 // 0101
3 // 0011
5 & 3 // 0001
 Bitwise OR (|)
 Bitwise XOR (^)
5 // 0101
3 // 0011
5 | 3 // 0111
5 // 0101
3 // 0011
5 ^ 3 // 0110
15
 Bit shifts are bitwise operations, where
 Bits are moved (shifted) to the left or right
 The bits that fall outside the number are
lost and replaced by 0
Bit Shifts
16
 Left shift (<< operator)  Right shift (>> operator)
0 1 1 1 1 1 1 1
1 1 1 1 1 1 1 0
0 1 1 1 1 1 1 1
0 0 1 1 1 1 1 1
 How to get the last bit from a number n?
 The bits are numbered from 0, from right to the left
 The position of the last (rightmost) bit is 0
 Last bit – formula:
Bitwise Operations: Get the Last Bit
n = 125 // 01111101
mask = 1 // & 00000001
n & mask // 00000001 = 1
17
lastBit = n & 1
1 1 1 0 10 1 1
4 3 2 1 07 6 5
n =
lastBit = 1
 How to get the bit at position p from a number n?
 Bit at position – formula:
Bitwise Operations: Get Bit at Position
n = 125 // 01111101
p = 5 // 5th position
125 >> p // 00000011 = 3
3 & 1 // 00000001 = 1
18
bit = (n >> p) & 1
1 1 1 0 10 1 1
4 3 2 1 07 6 5
n =
p = 5
bit value = 1
 How to set the bit at given position p to 0 or 1?
 Assign a bit b (0 or 1) at position p – formula:
Bitwise Operations: Set Bit at Position
p = 5 // 5th position
n = 125 // 01111101
mask = ~(1 << p) // 11011111
result = n & mask // 01011101
p = 5 // 5th position
n = 125 // 01111101
mask = 1 << p // 00100000
result = n | mask // 01111101
19
 Clear a bit (0) at position p  Set a bit (1) at position p
n = n & ~(1 << p) | (b << p)
20
 Networking protocols
 Many devices communicate using bit-level protocols
 E.g. the SYN flag in the TCP protocol header is the bit #1 from the
14th byte in the TCP packets
 Web browsers use bitwise operations to connect to a Web site
 Many binary file formats use bits to save space
 E.g. PNG images use 3 bits to specify the color format used
 Data compression replaces byte sequences with bit sequences
 E.g. the DEFLATE algorithm in ZIP files
Why We Need Bitwise Operations?
How to Become a
Software Engineer?
 First find out if programming is for you!
 Sign up for the SoftUni free coding course
for beginners: https://softuni.bg/apply
 Or follow a tutorial / book / video course on the Internet
 Does programming excite you?
 Do you really like it and enjoy it?
 Are you good at coding problems?
 Do you want to write code all the time?
How do I Become a Software Engineer?
22
 Coding skills – 20%
 Algorithmic thinking – 30%
 Fundamental concepts of the
software developer profession – 25%
 Programming languages &
software technologies – 25%
Learn the 4 of Fundamental Skills
23
 To become a software engineer, you need to study
hard and consistently for 3000+ hours!
 2 years @ 4 hours daily (average)
 Or 1 year @ 8-10 hours daily
 Developing practical projects
 Write 100K+ lines of code (LOC)
 100K LOC == 300 days * 350 LOC
Study Hard, Very Hard!
24
Learning Path: From Zero to First Job
25
https://softuni.bg/curriculum
Sign Up for SoftUni!
https://softuni.bg/apply
26
SoftUni – https://softuni.bg 2

More Related Content

What's hot

Unit-IV Windowing and Clipping.pdf
Unit-IV Windowing and Clipping.pdfUnit-IV Windowing and Clipping.pdf
Unit-IV Windowing and Clipping.pdfAmol Gaikwad
 
Ascii adjust & decimal adjust
Ascii adjust & decimal adjustAscii adjust & decimal adjust
Ascii adjust & decimal adjustTech_MX
 
Module 00 Bitwise Operators in C
Module 00 Bitwise Operators in CModule 00 Bitwise Operators in C
Module 00 Bitwise Operators in CTushar B Kute
 
Floating point presentation
Floating point presentationFloating point presentation
Floating point presentationSnehalataAgasti
 
Logical micro-operations
Logical micro-operationsLogical micro-operations
Logical micro-operationsVATSAL TRIVEDI
 
Multiplication algorithm, hardware and flowchart
Multiplication algorithm, hardware and flowchartMultiplication algorithm, hardware and flowchart
Multiplication algorithm, hardware and flowchartTanjarul Islam Mishu
 
1's and 2's complement.pptx
1's and 2's complement.pptx1's and 2's complement.pptx
1's and 2's complement.pptxHKShab
 
Sejarah perkembangan mikroprosesor
Sejarah perkembangan mikroprosesorSejarah perkembangan mikroprosesor
Sejarah perkembangan mikroprosesorAdola Silaban
 
Insertion operation in array(ds)
Insertion operation in array(ds)Insertion operation in array(ds)
Insertion operation in array(ds)chauhankapil
 

What's hot (20)

Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
C pointer
C pointerC pointer
C pointer
 
3D Transformation
3D Transformation3D Transformation
3D Transformation
 
Bit manipulation
Bit manipulationBit manipulation
Bit manipulation
 
Unit-IV Windowing and Clipping.pdf
Unit-IV Windowing and Clipping.pdfUnit-IV Windowing and Clipping.pdf
Unit-IV Windowing and Clipping.pdf
 
Ascii adjust & decimal adjust
Ascii adjust & decimal adjustAscii adjust & decimal adjust
Ascii adjust & decimal adjust
 
Module 00 Bitwise Operators in C
Module 00 Bitwise Operators in CModule 00 Bitwise Operators in C
Module 00 Bitwise Operators in C
 
Computer Science:Pointers in C
Computer Science:Pointers in CComputer Science:Pointers in C
Computer Science:Pointers in C
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Floating point presentation
Floating point presentationFloating point presentation
Floating point presentation
 
Python second ppt
Python second pptPython second ppt
Python second ppt
 
Floating Point Numbers
Floating Point NumbersFloating Point Numbers
Floating Point Numbers
 
Logical micro-operations
Logical micro-operationsLogical micro-operations
Logical micro-operations
 
Multiplication algorithm, hardware and flowchart
Multiplication algorithm, hardware and flowchartMultiplication algorithm, hardware and flowchart
Multiplication algorithm, hardware and flowchart
 
Operators in python
Operators in pythonOperators in python
Operators in python
 
1's and 2's complement.pptx
1's and 2's complement.pptx1's and 2's complement.pptx
1's and 2's complement.pptx
 
Sejarah perkembangan mikroprosesor
Sejarah perkembangan mikroprosesorSejarah perkembangan mikroprosesor
Sejarah perkembangan mikroprosesor
 
Insertion operation in array(ds)
Insertion operation in array(ds)Insertion operation in array(ds)
Insertion operation in array(ds)
 
Pointer to function 1
Pointer to function 1Pointer to function 1
Pointer to function 1
 
Singly linked list
Singly linked listSingly linked list
Singly linked list
 

Similar to Bitwise Operations in Programming

Similar to Bitwise Operations in Programming (20)

[ASM] Lab1
[ASM] Lab1[ASM] Lab1
[ASM] Lab1
 
DCF QNA edited
DCF QNA editedDCF QNA edited
DCF QNA edited
 
Comp Arithmetic Basic.ppt
Comp Arithmetic Basic.pptComp Arithmetic Basic.ppt
Comp Arithmetic Basic.ppt
 
04 chapter03 02_numbers_systems_student_version_fa16
04 chapter03 02_numbers_systems_student_version_fa1604 chapter03 02_numbers_systems_student_version_fa16
04 chapter03 02_numbers_systems_student_version_fa16
 
Int 2 data representation 2010
Int 2 data representation 2010Int 2 data representation 2010
Int 2 data representation 2010
 
1.Data information
1.Data information1.Data information
1.Data information
 
Chapter 1 digital design.pptx
Chapter 1 digital design.pptxChapter 1 digital design.pptx
Chapter 1 digital design.pptx
 
Operators expressions-and-statements
Operators expressions-and-statementsOperators expressions-and-statements
Operators expressions-and-statements
 
Process.org
Process.orgProcess.org
Process.org
 
Intro to assembly language
Intro to assembly languageIntro to assembly language
Intro to assembly language
 
Unit 2 Arithmetic
Unit 2 ArithmeticUnit 2 Arithmetic
Unit 2 Arithmetic
 
W 9 numbering system
W 9 numbering systemW 9 numbering system
W 9 numbering system
 
W 9 numbering system
W 9 numbering systemW 9 numbering system
W 9 numbering system
 
Data representation
Data representationData representation
Data representation
 
Number system and their conversion
Number system and their conversionNumber system and their conversion
Number system and their conversion
 
Neumerical Methods.pptx
Neumerical Methods.pptxNeumerical Methods.pptx
Neumerical Methods.pptx
 
Cit 1101 lec 02
Cit 1101 lec 02Cit 1101 lec 02
Cit 1101 lec 02
 
Data representation in computers
Data representation in computersData representation in computers
Data representation in computers
 
1663694151.pdf
1663694151.pdf1663694151.pdf
1663694151.pdf
 
NUMBER SYSTEM.pptx
NUMBER SYSTEM.pptxNUMBER SYSTEM.pptx
NUMBER SYSTEM.pptx
 

More from Svetlin Nakov

BG-IT-Edu: отворено учебно съдържание за ИТ учители
BG-IT-Edu: отворено учебно съдържание за ИТ учителиBG-IT-Edu: отворено учебно съдържание за ИТ учители
BG-IT-Edu: отворено учебно съдържание за ИТ учителиSvetlin Nakov
 
Programming World in 2024
Programming World in 2024Programming World in 2024
Programming World in 2024Svetlin Nakov
 
AI Tools for Business and Startups
AI Tools for Business and StartupsAI Tools for Business and Startups
AI Tools for Business and StartupsSvetlin Nakov
 
AI Tools for Scientists - Nakov (Oct 2023)
AI Tools for Scientists - Nakov (Oct 2023)AI Tools for Scientists - Nakov (Oct 2023)
AI Tools for Scientists - Nakov (Oct 2023)Svetlin Nakov
 
AI Tools for Entrepreneurs
AI Tools for EntrepreneursAI Tools for Entrepreneurs
AI Tools for EntrepreneursSvetlin Nakov
 
Bulgarian Tech Industry - Nakov at Dev.BG All in One Conference 2023
Bulgarian Tech Industry - Nakov at Dev.BG All in One Conference 2023Bulgarian Tech Industry - Nakov at Dev.BG All in One Conference 2023
Bulgarian Tech Industry - Nakov at Dev.BG All in One Conference 2023Svetlin Nakov
 
AI Tools for Business and Personal Life
AI Tools for Business and Personal LifeAI Tools for Business and Personal Life
AI Tools for Business and Personal LifeSvetlin Nakov
 
Дипломна работа: учебно съдържание по ООП - Светлин Наков
Дипломна работа: учебно съдържание по ООП - Светлин НаковДипломна работа: учебно съдържание по ООП - Светлин Наков
Дипломна работа: учебно съдържание по ООП - Светлин НаковSvetlin Nakov
 
Дипломна работа: учебно съдържание по ООП
Дипломна работа: учебно съдържание по ООПДипломна работа: учебно съдържание по ООП
Дипломна работа: учебно съдържание по ООПSvetlin Nakov
 
Свободно ИТ учебно съдържание за учители по програмиране и ИТ
Свободно ИТ учебно съдържание за учители по програмиране и ИТСвободно ИТ учебно съдържание за учители по програмиране и ИТ
Свободно ИТ учебно съдържание за учители по програмиране и ИТSvetlin Nakov
 
AI and the Professions of the Future
AI and the Professions of the FutureAI and the Professions of the Future
AI and the Professions of the FutureSvetlin Nakov
 
Programming Languages Trends for 2023
Programming Languages Trends for 2023Programming Languages Trends for 2023
Programming Languages Trends for 2023Svetlin Nakov
 
IT Professions and How to Become a Developer
IT Professions and How to Become a DeveloperIT Professions and How to Become a Developer
IT Professions and How to Become a DeveloperSvetlin Nakov
 
GitHub Actions (Nakov at RuseConf, Sept 2022)
GitHub Actions (Nakov at RuseConf, Sept 2022)GitHub Actions (Nakov at RuseConf, Sept 2022)
GitHub Actions (Nakov at RuseConf, Sept 2022)Svetlin Nakov
 
IT Professions and Their Future
IT Professions and Their FutureIT Professions and Their Future
IT Professions and Their FutureSvetlin Nakov
 
How to Become a QA Engineer and Start a Job
How to Become a QA Engineer and Start a JobHow to Become a QA Engineer and Start a Job
How to Become a QA Engineer and Start a JobSvetlin Nakov
 
Призвание и цели: моята рецепта
Призвание и цели: моята рецептаПризвание и цели: моята рецепта
Призвание и цели: моята рецептаSvetlin Nakov
 
What Mongolian IT Industry Can Learn from Bulgaria?
What Mongolian IT Industry Can Learn from Bulgaria?What Mongolian IT Industry Can Learn from Bulgaria?
What Mongolian IT Industry Can Learn from Bulgaria?Svetlin Nakov
 
How to Become a Software Developer - Nakov in Mongolia (Oct 2022)
How to Become a Software Developer - Nakov in Mongolia (Oct 2022)How to Become a Software Developer - Nakov in Mongolia (Oct 2022)
How to Become a Software Developer - Nakov in Mongolia (Oct 2022)Svetlin Nakov
 
Blockchain and DeFi Overview (Nakov, Sept 2021)
Blockchain and DeFi Overview (Nakov, Sept 2021)Blockchain and DeFi Overview (Nakov, Sept 2021)
Blockchain and DeFi Overview (Nakov, Sept 2021)Svetlin Nakov
 

More from Svetlin Nakov (20)

BG-IT-Edu: отворено учебно съдържание за ИТ учители
BG-IT-Edu: отворено учебно съдържание за ИТ учителиBG-IT-Edu: отворено учебно съдържание за ИТ учители
BG-IT-Edu: отворено учебно съдържание за ИТ учители
 
Programming World in 2024
Programming World in 2024Programming World in 2024
Programming World in 2024
 
AI Tools for Business and Startups
AI Tools for Business and StartupsAI Tools for Business and Startups
AI Tools for Business and Startups
 
AI Tools for Scientists - Nakov (Oct 2023)
AI Tools for Scientists - Nakov (Oct 2023)AI Tools for Scientists - Nakov (Oct 2023)
AI Tools for Scientists - Nakov (Oct 2023)
 
AI Tools for Entrepreneurs
AI Tools for EntrepreneursAI Tools for Entrepreneurs
AI Tools for Entrepreneurs
 
Bulgarian Tech Industry - Nakov at Dev.BG All in One Conference 2023
Bulgarian Tech Industry - Nakov at Dev.BG All in One Conference 2023Bulgarian Tech Industry - Nakov at Dev.BG All in One Conference 2023
Bulgarian Tech Industry - Nakov at Dev.BG All in One Conference 2023
 
AI Tools for Business and Personal Life
AI Tools for Business and Personal LifeAI Tools for Business and Personal Life
AI Tools for Business and Personal Life
 
Дипломна работа: учебно съдържание по ООП - Светлин Наков
Дипломна работа: учебно съдържание по ООП - Светлин НаковДипломна работа: учебно съдържание по ООП - Светлин Наков
Дипломна работа: учебно съдържание по ООП - Светлин Наков
 
Дипломна работа: учебно съдържание по ООП
Дипломна работа: учебно съдържание по ООПДипломна работа: учебно съдържание по ООП
Дипломна работа: учебно съдържание по ООП
 
Свободно ИТ учебно съдържание за учители по програмиране и ИТ
Свободно ИТ учебно съдържание за учители по програмиране и ИТСвободно ИТ учебно съдържание за учители по програмиране и ИТ
Свободно ИТ учебно съдържание за учители по програмиране и ИТ
 
AI and the Professions of the Future
AI and the Professions of the FutureAI and the Professions of the Future
AI and the Professions of the Future
 
Programming Languages Trends for 2023
Programming Languages Trends for 2023Programming Languages Trends for 2023
Programming Languages Trends for 2023
 
IT Professions and How to Become a Developer
IT Professions and How to Become a DeveloperIT Professions and How to Become a Developer
IT Professions and How to Become a Developer
 
GitHub Actions (Nakov at RuseConf, Sept 2022)
GitHub Actions (Nakov at RuseConf, Sept 2022)GitHub Actions (Nakov at RuseConf, Sept 2022)
GitHub Actions (Nakov at RuseConf, Sept 2022)
 
IT Professions and Their Future
IT Professions and Their FutureIT Professions and Their Future
IT Professions and Their Future
 
How to Become a QA Engineer and Start a Job
How to Become a QA Engineer and Start a JobHow to Become a QA Engineer and Start a Job
How to Become a QA Engineer and Start a Job
 
Призвание и цели: моята рецепта
Призвание и цели: моята рецептаПризвание и цели: моята рецепта
Призвание и цели: моята рецепта
 
What Mongolian IT Industry Can Learn from Bulgaria?
What Mongolian IT Industry Can Learn from Bulgaria?What Mongolian IT Industry Can Learn from Bulgaria?
What Mongolian IT Industry Can Learn from Bulgaria?
 
How to Become a Software Developer - Nakov in Mongolia (Oct 2022)
How to Become a Software Developer - Nakov in Mongolia (Oct 2022)How to Become a Software Developer - Nakov in Mongolia (Oct 2022)
How to Become a Software Developer - Nakov in Mongolia (Oct 2022)
 
Blockchain and DeFi Overview (Nakov, Sept 2021)
Blockchain and DeFi Overview (Nakov, Sept 2021)Blockchain and DeFi Overview (Nakov, Sept 2021)
Blockchain and DeFi Overview (Nakov, Sept 2021)
 

Recently uploaded

Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 

Recently uploaded (20)

Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 

Bitwise Operations in Programming

  • 1. https://softuni.org Processing Bits in the Integer Numbers Bitwise Operations in Programming Svetlin Nakov, PhD Co-Founder, Innovation and Inspiration Manager at SoftUni https://nakov.com
  • 3. 3  The 4 groups of software development skills  Coding, algorithms, development concepts, technologies  Numerals systems  Decimal, binary, hexadecimal  Conversion between numeral systems  Bitwise operations (&, I, ^, ~)  Processing bits in programming  Reading / writing bits from integers  How to become a software developer? Agenda
  • 4.  Coding skills – 20%  Algorithmic thinking – 30%  Fundamental concepts of the software developer profession – 25%  Programming languages & software technologies – 25% Skills of Software Developers 4
  • 6.  Bit == the smallest unit of data used in computing  Takes only one of two values: either a 0 or 1  1 bit can store anything with two separate states  Logical values (true / false)  Algebraic signs (+ / -)  Activation states (on / off)  Bits are organized in computer memory in sequences of 8 bits, called bytes (octets) Bit 6
  • 7.  Bit – single 0 or 1, representing a bit of data  Byte (octet) == 8 bits == the smallest addressable unit in the computer memory  KB (kilobyte) == 1024 bytes (sometimes 1000 bytes)  MB (megabyte) == 1024 KB == 1048576 bytes  GB (gigabyte) == 1024 MB == 1073741824 bytes  TB (terabyte) == 1024 GB == 1099511627776 bytes  PB (petabyte) == 1024 TB == 1125899906842624 bytes Bit, Byte, KB, MB, GB, TB, PB 7
  • 9.  Numeral system == system for representing numbers in written form using sequence of digits  Positional numeral systems == the value of each digit depends on its position  These numeral systems has a base (e.g. 2, 10, 16) Numeral Systems Decimal (base = 10) Binary (base = 2) Hexadecimal (base = 16) 30 111110 1E 45 101101 2D 60 111100 3C 9
  • 10. Decimal Numbers  Decimal numbers (base 10)  Represented using 10 numerals (called digits):  0, 1, 2, 3, 4, 5, 6, 7, 8, 9  Each position represents a power of 10 401 = 4*102 + 0*101 + 1*100 = = 4*100 + 0*10 + 1*1 = = 400 + 0 + 1 = 401 10
  • 11. Binary Numbers  The binary system is used in computer systems  Binary numbers (base 2)  Represented by sequence of 0 or 1  Each position represents a power of 2 5 -> 101b 101b = 1*22 + 0*21 + 1*20 = 4 + 0 + 1 = 5 11 1010b = 1*23 + 0*22 + 1*21 + 0*20 = 8 + 0 + 2 + 0 = 10
  • 12. Binary and Decimal Conversion  Binary to decimal  Multiply each digit to its magnitude (power of 2)  Decimal to binary  Divide to the base (2) until 0 is reached and take the reminders in reversed order 1011b = 1*23 + 0*22 + 1*21 + 1*20 = = 1*8 + 0*4 + 1*2 + 1*1 = = 8 + 0 + 2 + 1 = = 11 11 / 2 = 5 (1) // last digit 5 / 2 = 2 (1) // previous digit 2 / 2 = 1 (0) // previous digit 1 / 2 = 0 (1) // fist digit Result: 1011 12
  • 14. Bitwise Operators  Bitwise operators work with the binary representations of the numbers, applying bit by bit calculations  The operator ~ turns all 0 to 1 and all 1 to 0 (like ! for boolean expressions but bit by bit)  The operators |, & and ^ behave like ||, && and ^ for boolean expressions but bit by bit Operator | | | & & & ^ ^ ^ Operand1 0 1 1 0 1 1 0 0 1 Operand2 0 0 1 0 0 1 0 1 1 Result 0 1 1 0 0 1 0 1 0 14
  • 15.  Bitwise NOT (~)  Bitwise AND (&) Bitwise Operators – Examples 5 // 0101 ~5 // 1010 5 // 0101 3 // 0011 5 & 3 // 0001  Bitwise OR (|)  Bitwise XOR (^) 5 // 0101 3 // 0011 5 | 3 // 0111 5 // 0101 3 // 0011 5 ^ 3 // 0110 15
  • 16.  Bit shifts are bitwise operations, where  Bits are moved (shifted) to the left or right  The bits that fall outside the number are lost and replaced by 0 Bit Shifts 16  Left shift (<< operator)  Right shift (>> operator) 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1
  • 17.  How to get the last bit from a number n?  The bits are numbered from 0, from right to the left  The position of the last (rightmost) bit is 0  Last bit – formula: Bitwise Operations: Get the Last Bit n = 125 // 01111101 mask = 1 // & 00000001 n & mask // 00000001 = 1 17 lastBit = n & 1 1 1 1 0 10 1 1 4 3 2 1 07 6 5 n = lastBit = 1
  • 18.  How to get the bit at position p from a number n?  Bit at position – formula: Bitwise Operations: Get Bit at Position n = 125 // 01111101 p = 5 // 5th position 125 >> p // 00000011 = 3 3 & 1 // 00000001 = 1 18 bit = (n >> p) & 1 1 1 1 0 10 1 1 4 3 2 1 07 6 5 n = p = 5 bit value = 1
  • 19.  How to set the bit at given position p to 0 or 1?  Assign a bit b (0 or 1) at position p – formula: Bitwise Operations: Set Bit at Position p = 5 // 5th position n = 125 // 01111101 mask = ~(1 << p) // 11011111 result = n & mask // 01011101 p = 5 // 5th position n = 125 // 01111101 mask = 1 << p // 00100000 result = n | mask // 01111101 19  Clear a bit (0) at position p  Set a bit (1) at position p n = n & ~(1 << p) | (b << p)
  • 20. 20  Networking protocols  Many devices communicate using bit-level protocols  E.g. the SYN flag in the TCP protocol header is the bit #1 from the 14th byte in the TCP packets  Web browsers use bitwise operations to connect to a Web site  Many binary file formats use bits to save space  E.g. PNG images use 3 bits to specify the color format used  Data compression replaces byte sequences with bit sequences  E.g. the DEFLATE algorithm in ZIP files Why We Need Bitwise Operations?
  • 21. How to Become a Software Engineer?
  • 22.  First find out if programming is for you!  Sign up for the SoftUni free coding course for beginners: https://softuni.bg/apply  Or follow a tutorial / book / video course on the Internet  Does programming excite you?  Do you really like it and enjoy it?  Are you good at coding problems?  Do you want to write code all the time? How do I Become a Software Engineer? 22
  • 23.  Coding skills – 20%  Algorithmic thinking – 30%  Fundamental concepts of the software developer profession – 25%  Programming languages & software technologies – 25% Learn the 4 of Fundamental Skills 23
  • 24.  To become a software engineer, you need to study hard and consistently for 3000+ hours!  2 years @ 4 hours daily (average)  Or 1 year @ 8-10 hours daily  Developing practical projects  Write 100K+ lines of code (LOC)  100K LOC == 300 days * 350 LOC Study Hard, Very Hard! 24
  • 25. Learning Path: From Zero to First Job 25 https://softuni.bg/curriculum
  • 26. Sign Up for SoftUni! https://softuni.bg/apply 26