SlideShare a Scribd company logo
Algebra
• What is common between the following?
– 1 + 1 = 2
– 2 + 3 = 5
– 6 + 7 = 13
• Answer:
– a + b = c
What is the benefit of it?
Benefits Of Algebra
1. Readability
– Which one is easier to read?
I. The square feet of an area is the length
multiplied by the width
II. length * width = area
2. Easier calculation
– Sample question,
• We have ducks and lambs
• There is a total of 50 heads
• There is a total 150 feet
• How much ducks and how much lambs do we have?
Solution
1. Each duck has 1 head and 2 feet
2. Each lamb has 1 head and 4 feet
3. We got
I. Ducks + Lambs = 50 (heads)
II. (Ducks * 2) + (Lambs * 4) = 150 (feet)
4. Divide the second equation by 2
(Ducks * 2) + (Lambs * 4) = 150
/2
(Ducks) + (Lambs * 2) = 75
5. Subtract the first equation
(Ducks) + (Lambs * 2) = 75
- Ducks + Lambs = 50
Lambs = 50
Algebra Functions
A function in algebra is:
1. A formula
2. That returns a value
3. That can take arguments
4. The return value is always the same, if the
arguments are the same
Example:
1. Add(a,b) is a function
2. CurrentTime() is not a function
3. Void DoSomethingNoReturn is not a function
Algebra function syntax:
area = f(length, width) = length * width
Functions In Computers
• Function vs Sub
– Databases separate between a function (that returns
a value) and stored procedures (that is like a
subprogram but doesn’t return a value)
– Also basic distinguishes between a function that
returns a value and a “sub” (subroutine or
subprogram) that doesn’t return a value
– Other imperative languages consider everything as
functions
• Same return value
– MySQL has a keyword “deterministic”
– In functional languages a variable is not changeable,
and a function always returns the same value (as in
algebra)
• A function with no arguments is in fact a constant
Boolean Algebra
• True = 1
• False = 0
• True AND True = True
• True AND False = False
• True OR False = True
• True Xor True = False
• True Xor False = True
• Not True = False
• Not False = True
Computers And Boolean Algebra
1. If and while statements
2. The hardware logic gates are based on
“Nand” and “Not” circut transistors
Computer Language history
• Machine Language
– Assembly Language
• Fortran (FORmula TRANsalator)
– Algol
» B
• C (UNIX) by K&R
• C++ [c = c + 1]
• Java
• C#
• JavaScript
• PHP
• Basic
– VB6
– VBA
– VBS
– VB.Net
Sample Memory Transistor
“Not” (Inverter)
Chip Is On (1)
On (1) Off (0) – We ignore this side
“Not” (Inverter)
“Not” (Inverter)
Chip Is Off (0)
Off (0) On (1) – We ignore this side
“Not” (Inverter)
Program Internal
Sections
.text.data.bss
Stack
Virtual Memory Layout
Operating
System
Reserved
.data
Break
(Heap)
.text
.bss
S
t
a
c
k
0
FFFFFFFF
How The Sections Work
.text
.data
Instruction1
Instruction2
OpCode Operand1 Operand2
ADD 01010101 11110000
a = 1
b = 1
a + b
00000001
00000001
Variable a @(01010101)
Variable b @ (11110000)
Code
Indirection
.text
.dataInstruction1
Instruction2
OpCode Operand1 Operand2
ADD 11110011 11111111
a = 1
b = 1
c* = &a
d* = &b
c* + d*
00000001
00000001
Variable a @(01010101)
Variable b @ (11110000)Code
01010101Variable c @ (11110011)
11110000Variable d @ (11111111)
I
n
d
i
r
e
c
t
Example A “C” “Array”
myArray = array()
i = 1
myArray[i] = 0
00000001
i @(01010101)
myArray @ (11110000)
Code
myArray[i] == myArray[1] == myArray + 1 @ (11110001) 00000000
Index value
(indirection)
Back To Memory
a = 0
p* =&a
11110001
Whole_Memory_Array
Pointer p @(01010101)
Code
a =Actual pointed variable = Whole_Memory_Array[p]@ (11110001) 00000000
Pointer value
(indirection)
Intro To Binary
Switch = Bit
8 Switches = 8 Bits = Byte
1 = On
0 = Off
We can have only 0 or 1
Is there any way to get real numbers?
Lets Take an Example From Regular
(Decimal) Numbers
• 0
• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• ?????
We have only 9 numerals
So how do we proceed?
Lets Take an Example From Regular
(Decimal) Numbers
• 0
• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• ?????
The answer is combination
• 10
• 11
• 12
• …
• 20
• 21
• ….
• 30
• ….
• 99
• ?????
• 100
• 101
• ….
• 200
• …..
Back To Binary
• 0
• 1
• ???
The answer is combination
• 10
• 11
• ?????
• 100
• 101
• 110
• 111
• ??????
• 1000
• 1001
• 1010
• 1100
• 1101
• 1110
• 1111
• ??????
Binary Compared To Decimal
• 0
• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 8
• 9
• ……
• 00000000
• 00000001
• 00000010
• 00000011
• 00000100
• 00000101
• 00000110
• 00000111
• 00001000
• 00001001
• ……..
• 0
• 1
• 2
• 3
• 4
• 5
• 6
• 7
• 10
• 11
• ……
Decimal Binary Octal
Negative Numbers
• Rule 1: Negative has a 1 in front
Ex: 100000001
• Rule 2: The 2’s complement
1. The 1’s Complement – Xor all bits
Ex: 000000001 - (Decimal “1”)
Xor: 111111110
2. The 2’s Complement – Add 1
Ex: 000000001 - (Decimal “1”)
Xor: 111111110
Add 1: 111111110 - (Decimal “-1”)
Converting Between Smaller And Larger Types
byte a = 1
short b = a
Code
00000001
0000000000000001
Correct
unsigned byte a = 255
short b = a
11111111
0000000011111111
Correct
byte a = -1
short b = a
11111111
0000000011111111
Wrong
11111111
1111111111111111Correct
Positive Values
Negative Values
Identifiers Turned Into Memory
Addresses
1. The identifiers that are being turned into memory
addresses:
– Global Variables
– Functions
– Labels
2. The identifiers that are NOT being turned into memory
addresses, (are only used to measure the size to reserve):
– Custom types
– Struct
– Class names
3. The identifiers that are used as offsets:
– Array index
– Class (and struct) field (NOT function) member
– Local variables
Variables
• Size of the reserved memory is based on the type
• There might be the following types
1. Built-in type – which is just a group of bytes, based on the compiler
and/or platform
2. Custom type – which is just a series of built in types
3. Array (in C and C++) – which is just a series of the same built-in type
(but no one keeps track of the length, so it is the programmers job)
4. Pointer – system defined to hold memory addresses
5. Reference – a pointer without the possibility to access the memory
address
6. Handle – a value supplied by the operating system (like the index of an
array)
7. Typedef and Define – available in C and C++ to rename existing types
Variables are a “higher language – human
readable” name for a memory address
Labels
• There is no size associated with it
• Location
– In assembly it might be everywhere
• In fact in assembly it is generally the only way to declare a
variable, function, loop, or “else”
• In Dos batch it is the only way to declare a function
– In C and C++ it might be only in a function – but is only
recommended to break out of a nested loop
– In VB it is used for error handling “On error goto”
– In Java it might only be before a loop
Labels are a “higher language – human
readable” name for a memory address
Label Sample In Assembly
.data
.int
var1:
1
var2:
10
.text
.global
start:
mov var1 %eax
call myfunc
jmp myfunc
myfunc:
mov var2 %ebx
add %eax %ebx
ret
Label Sample In Java (Or C)
outerLabel:
while(1==1)
{
while(2==2)
{
//Java syntax
break outerLabel;
//C syntax (not the same as
before, as it will cause the loop again)
goto outerLabel;
}
}
Boolean Type
• False == 0 (all switches are of)
• True == 1 (switch is on, and also matches
Boolean algebra)
• All other numbers are also considered true (as
there are switches on)
• There are languages that require conversion
between numbers and Boolean (and other are
doing it behind the scenes))
However TWO languages are an
exception
Why Some Languages Require conversion
•Consider the following C code, all of them are perfectly valid:
if(1==1) //true
if(1) //true as well
if(a) //Checks if “a” is non-zero
if(a==b) //Compares “a” to “b”
if(a=b) //Sets “a” to the value of “b”, and then
//checks “a” if it is non-zero, Is this by
//intention or typo?
•However in Java the last statement would not compile, as it is
not a Boolean operation
•For C there is some avoidance by having constants to the left, ie.
if(20==a) Instead of if(a==20)
Because
if(20=a) Is a syntax error
While
if(a=20) Is perfectly valid
Implicit Conversion
• However some languages employ implicit
conversion
• JavaScript considers
– If (1) : true
– If (0) : false
– If (“”) : false
– If (“0”) : true
• Php Considers
• If (“0”) : false
• If (array()) : false
Boolean In Visual Basic
• True in VB = -1
• To see why let us see it in binary
– 1 = 00000001
– 1’s complement = 1111111110
– 2’s complement = 1111111111
• So all switches are on
But why different than all others?
To answer that we need to understand the difference
between Logical and Bitwise operators, and why do
Logical operators short circuit?
Logical vs bitwise
• Logical
– Step 1 – Check the left side for true
– Step 2 – If still no conclusion check the right side
– Step 3 – Compare both sides and give the answer
• Bitwise
– Step 1 – Translate both sides into binary
– Step 2 – Compare both sides bit per bit
– Step 3 – Provide the soltuion
Example Bitwise vs Logical
• Example 1
If 1==1 AND 2==2
Logical And Bitwise And
Step 1: 1==1 is true 1==1 is true=00000001
Step 2: 2 ==2 is true 2==2 is true=00000001
Step 3:
True 00000001
And True 00000001
= True 00000001
More Examples
• Example 2
If 1==2 AND 2==2
Logical And Bitwise And
Step 1: 1==2 is false 1==2 is false=00000000
Step 2: return false 2==2 is true =00000001
Step 3: N/A 00000000
AND 00000001
00000000
• Example 3
If 1 AND 2
Step 1: 1 is True 00000001
Step 2: 2 is True 00000010
Step 3: True 00000000
Bitwise vs Logical Operators
Operator C Basic VB.Net
(Logical)
Logical AND && N/A AndAlso
Logical OR || N/A OrElse
Logical NOT ! N/A N/A
(Bitwise)
Bitwise AND & AND AND
Bitwise OR | OR OR
Bitwise XOR ^ XOR XOR
Bitwise NOT ~ NOT NOT
Back To VB
• Since we have only a bitwise NOT we have to
make sure it works on Boolean
NOT On 1
1 = 00000001 = True
NOT = 11111110 = True
NOT On -1
-1 = 11111111 = True
NOT = 00000000 = False
Beware Of The Win32 API
Boolean In Bash Shell Scripting
# if(true) then echo “works”; fi
# works
#
# if(false) then echo “works”; fi
#
# if(test 1 –eq 1) then echo “works”; fi
# works
#
# if(test 1 –eq 2) then echo “works”; fi
#
# echo test 1 –eq 1
#
# test 1 –eq 1
# echo $?
# 0
# test 1 –eq 2
# echo $?
# 1
#
# true
# echo #?
# 0
# false
# echo #?
# 1

More Related Content

What's hot (20)

PPTX
L2 datatypes and variables
teach4uin
 
PPTX
Python language data types
Hoang Nguyen
 
PPTX
Python programming
saroja20
 
PPSX
Programming with Python
Rasan Samarasinghe
 
PDF
Python revision tour II
Mr. Vikram Singh Slathia
 
PDF
Aaa ped-3. Pythond: advanced concepts
AminaRepo
 
PPTX
Ruby data types and objects
Harkamal Singh
 
PPTX
Python Data-Types
Akhil Kaushik
 
PDF
Lesson 03 python statement, indentation and comments
Nilimesh Halder
 
PPTX
Python basics
Manisha Gholve
 
PPT
Introduction to Python - Part Three
amiable_indian
 
PPTX
An Introduction : Python
Raghu Kumar
 
PDF
C# Programming: Fundamentals
Mahmoud Abdallah
 
PDF
Oop with c++ notes unit 01 introduction
Ananda Kumar HN
 
PDF
Python-01| Fundamentals
Mohd Sajjad
 
PPT
M C6java3
mbruggen
 
PPTX
PPT on Data Science Using Python
NishantKumar1179
 
DOCX
Notes on c++
Selvam Edwin
 
PPTX
Full Python in 20 slides
rfojdar
 
PDF
Generics
Ravi_Kant_Sahu
 
L2 datatypes and variables
teach4uin
 
Python language data types
Hoang Nguyen
 
Python programming
saroja20
 
Programming with Python
Rasan Samarasinghe
 
Python revision tour II
Mr. Vikram Singh Slathia
 
Aaa ped-3. Pythond: advanced concepts
AminaRepo
 
Ruby data types and objects
Harkamal Singh
 
Python Data-Types
Akhil Kaushik
 
Lesson 03 python statement, indentation and comments
Nilimesh Halder
 
Python basics
Manisha Gholve
 
Introduction to Python - Part Three
amiable_indian
 
An Introduction : Python
Raghu Kumar
 
C# Programming: Fundamentals
Mahmoud Abdallah
 
Oop with c++ notes unit 01 introduction
Ananda Kumar HN
 
Python-01| Fundamentals
Mohd Sajjad
 
M C6java3
mbruggen
 
PPT on Data Science Using Python
NishantKumar1179
 
Notes on c++
Selvam Edwin
 
Full Python in 20 slides
rfojdar
 
Generics
Ravi_Kant_Sahu
 

Viewers also liked (10)

PPTX
K map
Abhijit Jadhav
 
PPT
Karnaugh maps
Gaditek
 
PPTX
Karnaugh Maps
Ammara Javed
 
PPT
KARNAUGH MAP(K-MAP)
mihir jain
 
PPTX
K - Map
Abhishek Choksi
 
PPTX
K map
deepak15015
 
PPT
Digital Logic & Design (DLD) presentation
foyez ahammad
 
PPT
DLD Practical Lab Work
Bachagul Ghaljai
 
PPT
Karnaugh map
Vanitha Chandru
 
PPT
Basics Of Semiconductor Memories
Rahul Bandhe
 
Karnaugh maps
Gaditek
 
Karnaugh Maps
Ammara Javed
 
KARNAUGH MAP(K-MAP)
mihir jain
 
K - Map
Abhishek Choksi
 
Digital Logic & Design (DLD) presentation
foyez ahammad
 
DLD Practical Lab Work
Bachagul Ghaljai
 
Karnaugh map
Vanitha Chandru
 
Basics Of Semiconductor Memories
Rahul Bandhe
 
Ad

Similar to [YIDLUG] Programming Languages Differences, The Underlying Implementation 1 of 2 (20)

PPT
C tutorial
Anurag Sukhija
 
PPS
Programming in Arduino (Part 1)
Niket Chandrawanshi
 
PDF
C language
Mohamed Bedair
 
PPT
c_tutorial_2.ppt
gitesh_nagar
 
PPT
C C++ tutorial for beginners- tibacademy.in
TIB Academy
 
PPTX
Lecture 02 Programming C for Beginners 001
MahmoudElsamanty
 
PPTX
Introduction to c
Veeresh Metigoudar
 
PDF
Mit6 087 iap10_lec02
John Lawrence
 
PDF
Module 2_PPT_P1 POP Notes module 2 fdfd.pdf
anilcsbs
 
PPTX
Fundamentals of computers - C Programming
MSridhar18
 
PPTX
BCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptx
RutviBaraiya
 
PPTX
introduction to c programming and C History.pptx
ManojKhadilkar1
 
PPTX
C Session 2.pptx for engninering students
nraj02252
 
PPT
Low Level Prog. (from 201-c).ppt
LearnWithJCM
 
PPTX
C language basics
Milind Deshkar
 
PPT
Fundamentals of c programming techniques
VenkataRangaRaoKommi1
 
PPT
Introduction to c
sunila tharagaturi
 
PDF
Introduction of c_language
SINGH PROJECTS
 
C tutorial
Anurag Sukhija
 
Programming in Arduino (Part 1)
Niket Chandrawanshi
 
C language
Mohamed Bedair
 
c_tutorial_2.ppt
gitesh_nagar
 
C C++ tutorial for beginners- tibacademy.in
TIB Academy
 
Lecture 02 Programming C for Beginners 001
MahmoudElsamanty
 
Introduction to c
Veeresh Metigoudar
 
Mit6 087 iap10_lec02
John Lawrence
 
Module 2_PPT_P1 POP Notes module 2 fdfd.pdf
anilcsbs
 
Fundamentals of computers - C Programming
MSridhar18
 
BCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptx
RutviBaraiya
 
introduction to c programming and C History.pptx
ManojKhadilkar1
 
C Session 2.pptx for engninering students
nraj02252
 
Low Level Prog. (from 201-c).ppt
LearnWithJCM
 
C language basics
Milind Deshkar
 
Fundamentals of c programming techniques
VenkataRangaRaoKommi1
 
Introduction to c
sunila tharagaturi
 
Introduction of c_language
SINGH PROJECTS
 
Ad

Recently uploaded (20)

PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PPTX
Top Managed Service Providers in Los Angeles
Captain IT
 
PDF
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PPTX
Extensions Framework (XaaS) - Enabling Orchestrate Anything
ShapeBlue
 
PDF
Apache CloudStack 201: Let's Design & Build an IaaS Cloud
ShapeBlue
 
PDF
Sustainable and comertially viable mining process.pdf
Avijit Kumar Roy
 
PPTX
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
PDF
Upgrading to z_OS V2R4 Part 01 of 02.pdf
Flavio787771
 
PDF
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
PPT
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
PPTX
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
PDF
Meetup Kickoff & Welcome - Rohit Yadav, CSIUG Chairman
ShapeBlue
 
PDF
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
PDF
Upskill to Agentic Automation 2025 - Kickoff Meeting
DianaGray10
 
PDF
Novus Safe Lite- What is Novus Safe Lite.pdf
Novus Hi-Tech
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
Top Managed Service Providers in Los Angeles
Captain IT
 
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
Extensions Framework (XaaS) - Enabling Orchestrate Anything
ShapeBlue
 
Apache CloudStack 201: Let's Design & Build an IaaS Cloud
ShapeBlue
 
Sustainable and comertially viable mining process.pdf
Avijit Kumar Roy
 
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
Upgrading to z_OS V2R4 Part 01 of 02.pdf
Flavio787771
 
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
Meetup Kickoff & Welcome - Rohit Yadav, CSIUG Chairman
ShapeBlue
 
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
Upskill to Agentic Automation 2025 - Kickoff Meeting
DianaGray10
 
Novus Safe Lite- What is Novus Safe Lite.pdf
Novus Hi-Tech
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 

[YIDLUG] Programming Languages Differences, The Underlying Implementation 1 of 2

  • 1. Algebra • What is common between the following? – 1 + 1 = 2 – 2 + 3 = 5 – 6 + 7 = 13 • Answer: – a + b = c What is the benefit of it?
  • 2. Benefits Of Algebra 1. Readability – Which one is easier to read? I. The square feet of an area is the length multiplied by the width II. length * width = area 2. Easier calculation – Sample question, • We have ducks and lambs • There is a total of 50 heads • There is a total 150 feet • How much ducks and how much lambs do we have?
  • 3. Solution 1. Each duck has 1 head and 2 feet 2. Each lamb has 1 head and 4 feet 3. We got I. Ducks + Lambs = 50 (heads) II. (Ducks * 2) + (Lambs * 4) = 150 (feet) 4. Divide the second equation by 2 (Ducks * 2) + (Lambs * 4) = 150 /2 (Ducks) + (Lambs * 2) = 75 5. Subtract the first equation (Ducks) + (Lambs * 2) = 75 - Ducks + Lambs = 50 Lambs = 50
  • 4. Algebra Functions A function in algebra is: 1. A formula 2. That returns a value 3. That can take arguments 4. The return value is always the same, if the arguments are the same Example: 1. Add(a,b) is a function 2. CurrentTime() is not a function 3. Void DoSomethingNoReturn is not a function Algebra function syntax: area = f(length, width) = length * width
  • 5. Functions In Computers • Function vs Sub – Databases separate between a function (that returns a value) and stored procedures (that is like a subprogram but doesn’t return a value) – Also basic distinguishes between a function that returns a value and a “sub” (subroutine or subprogram) that doesn’t return a value – Other imperative languages consider everything as functions • Same return value – MySQL has a keyword “deterministic” – In functional languages a variable is not changeable, and a function always returns the same value (as in algebra) • A function with no arguments is in fact a constant
  • 6. Boolean Algebra • True = 1 • False = 0 • True AND True = True • True AND False = False • True OR False = True • True Xor True = False • True Xor False = True • Not True = False • Not False = True
  • 7. Computers And Boolean Algebra 1. If and while statements 2. The hardware logic gates are based on “Nand” and “Not” circut transistors
  • 8. Computer Language history • Machine Language – Assembly Language • Fortran (FORmula TRANsalator) – Algol » B • C (UNIX) by K&R • C++ [c = c + 1] • Java • C# • JavaScript • PHP • Basic – VB6 – VBA – VBS – VB.Net
  • 9. Sample Memory Transistor “Not” (Inverter) Chip Is On (1) On (1) Off (0) – We ignore this side “Not” (Inverter) “Not” (Inverter) Chip Is Off (0) Off (0) On (1) – We ignore this side “Not” (Inverter)
  • 12. How The Sections Work .text .data Instruction1 Instruction2 OpCode Operand1 Operand2 ADD 01010101 11110000 a = 1 b = 1 a + b 00000001 00000001 Variable a @(01010101) Variable b @ (11110000) Code
  • 13. Indirection .text .dataInstruction1 Instruction2 OpCode Operand1 Operand2 ADD 11110011 11111111 a = 1 b = 1 c* = &a d* = &b c* + d* 00000001 00000001 Variable a @(01010101) Variable b @ (11110000)Code 01010101Variable c @ (11110011) 11110000Variable d @ (11111111) I n d i r e c t
  • 14. Example A “C” “Array” myArray = array() i = 1 myArray[i] = 0 00000001 i @(01010101) myArray @ (11110000) Code myArray[i] == myArray[1] == myArray + 1 @ (11110001) 00000000 Index value (indirection)
  • 15. Back To Memory a = 0 p* =&a 11110001 Whole_Memory_Array Pointer p @(01010101) Code a =Actual pointed variable = Whole_Memory_Array[p]@ (11110001) 00000000 Pointer value (indirection)
  • 16. Intro To Binary Switch = Bit 8 Switches = 8 Bits = Byte 1 = On 0 = Off We can have only 0 or 1 Is there any way to get real numbers?
  • 17. Lets Take an Example From Regular (Decimal) Numbers • 0 • 1 • 2 • 3 • 4 • 5 • 6 • 7 • 8 • 9 • ????? We have only 9 numerals So how do we proceed?
  • 18. Lets Take an Example From Regular (Decimal) Numbers • 0 • 1 • 2 • 3 • 4 • 5 • 6 • 7 • 8 • 9 • ????? The answer is combination • 10 • 11 • 12 • … • 20 • 21 • …. • 30 • …. • 99 • ????? • 100 • 101 • …. • 200 • …..
  • 19. Back To Binary • 0 • 1 • ??? The answer is combination • 10 • 11 • ????? • 100 • 101 • 110 • 111 • ?????? • 1000 • 1001 • 1010 • 1100 • 1101 • 1110 • 1111 • ??????
  • 20. Binary Compared To Decimal • 0 • 1 • 2 • 3 • 4 • 5 • 6 • 7 • 8 • 9 • …… • 00000000 • 00000001 • 00000010 • 00000011 • 00000100 • 00000101 • 00000110 • 00000111 • 00001000 • 00001001 • …….. • 0 • 1 • 2 • 3 • 4 • 5 • 6 • 7 • 10 • 11 • …… Decimal Binary Octal
  • 21. Negative Numbers • Rule 1: Negative has a 1 in front Ex: 100000001 • Rule 2: The 2’s complement 1. The 1’s Complement – Xor all bits Ex: 000000001 - (Decimal “1”) Xor: 111111110 2. The 2’s Complement – Add 1 Ex: 000000001 - (Decimal “1”) Xor: 111111110 Add 1: 111111110 - (Decimal “-1”)
  • 22. Converting Between Smaller And Larger Types byte a = 1 short b = a Code 00000001 0000000000000001 Correct unsigned byte a = 255 short b = a 11111111 0000000011111111 Correct byte a = -1 short b = a 11111111 0000000011111111 Wrong 11111111 1111111111111111Correct Positive Values Negative Values
  • 23. Identifiers Turned Into Memory Addresses 1. The identifiers that are being turned into memory addresses: – Global Variables – Functions – Labels 2. The identifiers that are NOT being turned into memory addresses, (are only used to measure the size to reserve): – Custom types – Struct – Class names 3. The identifiers that are used as offsets: – Array index – Class (and struct) field (NOT function) member – Local variables
  • 24. Variables • Size of the reserved memory is based on the type • There might be the following types 1. Built-in type – which is just a group of bytes, based on the compiler and/or platform 2. Custom type – which is just a series of built in types 3. Array (in C and C++) – which is just a series of the same built-in type (but no one keeps track of the length, so it is the programmers job) 4. Pointer – system defined to hold memory addresses 5. Reference – a pointer without the possibility to access the memory address 6. Handle – a value supplied by the operating system (like the index of an array) 7. Typedef and Define – available in C and C++ to rename existing types Variables are a “higher language – human readable” name for a memory address
  • 25. Labels • There is no size associated with it • Location – In assembly it might be everywhere • In fact in assembly it is generally the only way to declare a variable, function, loop, or “else” • In Dos batch it is the only way to declare a function – In C and C++ it might be only in a function – but is only recommended to break out of a nested loop – In VB it is used for error handling “On error goto” – In Java it might only be before a loop Labels are a “higher language – human readable” name for a memory address
  • 26. Label Sample In Assembly .data .int var1: 1 var2: 10 .text .global start: mov var1 %eax call myfunc jmp myfunc myfunc: mov var2 %ebx add %eax %ebx ret
  • 27. Label Sample In Java (Or C) outerLabel: while(1==1) { while(2==2) { //Java syntax break outerLabel; //C syntax (not the same as before, as it will cause the loop again) goto outerLabel; } }
  • 28. Boolean Type • False == 0 (all switches are of) • True == 1 (switch is on, and also matches Boolean algebra) • All other numbers are also considered true (as there are switches on) • There are languages that require conversion between numbers and Boolean (and other are doing it behind the scenes)) However TWO languages are an exception
  • 29. Why Some Languages Require conversion •Consider the following C code, all of them are perfectly valid: if(1==1) //true if(1) //true as well if(a) //Checks if “a” is non-zero if(a==b) //Compares “a” to “b” if(a=b) //Sets “a” to the value of “b”, and then //checks “a” if it is non-zero, Is this by //intention or typo? •However in Java the last statement would not compile, as it is not a Boolean operation •For C there is some avoidance by having constants to the left, ie. if(20==a) Instead of if(a==20) Because if(20=a) Is a syntax error While if(a=20) Is perfectly valid
  • 30. Implicit Conversion • However some languages employ implicit conversion • JavaScript considers – If (1) : true – If (0) : false – If (“”) : false – If (“0”) : true • Php Considers • If (“0”) : false • If (array()) : false
  • 31. Boolean In Visual Basic • True in VB = -1 • To see why let us see it in binary – 1 = 00000001 – 1’s complement = 1111111110 – 2’s complement = 1111111111 • So all switches are on But why different than all others? To answer that we need to understand the difference between Logical and Bitwise operators, and why do Logical operators short circuit?
  • 32. Logical vs bitwise • Logical – Step 1 – Check the left side for true – Step 2 – If still no conclusion check the right side – Step 3 – Compare both sides and give the answer • Bitwise – Step 1 – Translate both sides into binary – Step 2 – Compare both sides bit per bit – Step 3 – Provide the soltuion
  • 33. Example Bitwise vs Logical • Example 1 If 1==1 AND 2==2 Logical And Bitwise And Step 1: 1==1 is true 1==1 is true=00000001 Step 2: 2 ==2 is true 2==2 is true=00000001 Step 3: True 00000001 And True 00000001 = True 00000001
  • 34. More Examples • Example 2 If 1==2 AND 2==2 Logical And Bitwise And Step 1: 1==2 is false 1==2 is false=00000000 Step 2: return false 2==2 is true =00000001 Step 3: N/A 00000000 AND 00000001 00000000 • Example 3 If 1 AND 2 Step 1: 1 is True 00000001 Step 2: 2 is True 00000010 Step 3: True 00000000
  • 35. Bitwise vs Logical Operators Operator C Basic VB.Net (Logical) Logical AND && N/A AndAlso Logical OR || N/A OrElse Logical NOT ! N/A N/A (Bitwise) Bitwise AND & AND AND Bitwise OR | OR OR Bitwise XOR ^ XOR XOR Bitwise NOT ~ NOT NOT
  • 36. Back To VB • Since we have only a bitwise NOT we have to make sure it works on Boolean NOT On 1 1 = 00000001 = True NOT = 11111110 = True NOT On -1 -1 = 11111111 = True NOT = 00000000 = False Beware Of The Win32 API
  • 37. Boolean In Bash Shell Scripting # if(true) then echo “works”; fi # works # # if(false) then echo “works”; fi # # if(test 1 –eq 1) then echo “works”; fi # works # # if(test 1 –eq 2) then echo “works”; fi # # echo test 1 –eq 1 # # test 1 –eq 1 # echo $? # 0 # test 1 –eq 2 # echo $? # 1 # # true # echo #? # 0 # false # echo #? # 1