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

PPTX
Introduction to the basics of Python programming (part 1)
PPT
Python
PPSX
DISE - Windows Based Application Development in Java
ODP
Preparing Java 7 Certifications
PPTX
Python basics
PDF
Python unit 1 part-2
PPTX
Java Foundations: Methods
PPT
Python programming
Introduction to the basics of Python programming (part 1)
Python
DISE - Windows Based Application Development in Java
Preparing Java 7 Certifications
Python basics
Python unit 1 part-2
Java Foundations: Methods
Python programming

What's hot (19)

PPTX
L2 datatypes and variables
PPTX
Python language data types
PPTX
Python programming
PPSX
Programming with Python
PDF
Python revision tour II
PDF
Aaa ped-3. Pythond: advanced concepts
PPTX
Ruby data types and objects
PPTX
Python Data-Types
PDF
Lesson 03 python statement, indentation and comments
PPTX
Python basics
PPT
Introduction to Python - Part Three
PPTX
An Introduction : Python
PDF
Oop with c++ notes unit 01 introduction
PDF
Python-01| Fundamentals
PPT
M C6java3
PPTX
PPT on Data Science Using Python
DOCX
Notes on c++
PPTX
Full Python in 20 slides
PDF
Generics
L2 datatypes and variables
Python language data types
Python programming
Programming with Python
Python revision tour II
Aaa ped-3. Pythond: advanced concepts
Ruby data types and objects
Python Data-Types
Lesson 03 python statement, indentation and comments
Python basics
Introduction to Python - Part Three
An Introduction : Python
Oop with c++ notes unit 01 introduction
Python-01| Fundamentals
M C6java3
PPT on Data Science Using Python
Notes on c++
Full Python in 20 slides
Generics
Ad

Viewers also liked (10)

PPTX
PPT
Karnaugh maps
PPTX
Karnaugh Maps
PPT
KARNAUGH MAP(K-MAP)
PPTX
K - Map
PPTX
PPT
Digital Logic & Design (DLD) presentation
PPT
DLD Practical Lab Work
PPT
Karnaugh map
PPT
Basics Of Semiconductor Memories
Karnaugh maps
Karnaugh Maps
KARNAUGH MAP(K-MAP)
K - Map
Digital Logic & Design (DLD) presentation
DLD Practical Lab Work
Karnaugh map
Basics Of Semiconductor Memories
Ad

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

PPT
C tutorial
PPS
Programming in Arduino (Part 1)
PDF
C language
PPT
c_tutorial_2.ppt
PPT
C C++ tutorial for beginners- tibacademy.in
PPTX
Lecture 02 Programming C for Beginners 001
PPTX
Introduction to c
PDF
Mit6 087 iap10_lec02
PDF
Module 2_PPT_P1 POP Notes module 2 fdfd.pdf
PPTX
Fundamentals of computers - C Programming
PPTX
BCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptx
PPTX
introduction to c programming and C History.pptx
PPTX
C Session 2.pptx for engninering students
PPT
Low Level Prog. (from 201-c).ppt
PPTX
C language basics
PPT
Fundamentals of c programming techniques
PPT
Introduction to c
PDF
Introduction of c_language
C tutorial
Programming in Arduino (Part 1)
C language
c_tutorial_2.ppt
C C++ tutorial for beginners- tibacademy.in
Lecture 02 Programming C for Beginners 001
Introduction to c
Mit6 087 iap10_lec02
Module 2_PPT_P1 POP Notes module 2 fdfd.pdf
Fundamentals of computers - C Programming
BCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptx
introduction to c programming and C History.pptx
C Session 2.pptx for engninering students
Low Level Prog. (from 201-c).ppt
C language basics
Fundamentals of c programming techniques
Introduction to c
Introduction of c_language

Recently uploaded (20)

PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Cloud computing and distributed systems.
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
cuic standard and advanced reporting.pdf
PPTX
Spectroscopy.pptx food analysis technology
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Encapsulation theory and applications.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
MIND Revenue Release Quarter 2 2025 Press Release
The Rise and Fall of 3GPP – Time for a Sabbatical?
NewMind AI Weekly Chronicles - August'25 Week I
Programs and apps: productivity, graphics, security and other tools
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Advanced methodologies resolving dimensionality complications for autism neur...
Cloud computing and distributed systems.
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
cuic standard and advanced reporting.pdf
Spectroscopy.pptx food analysis technology
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Encapsulation theory and applications.pdf
Network Security Unit 5.pdf for BCA BBA.
The AUB Centre for AI in Media Proposal.docx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Understanding_Digital_Forensics_Presentation.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Review of recent advances in non-invasive hemoglobin estimation
MIND Revenue Release Quarter 2 2025 Press Release

[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