Binary World

Download Report

Transcript Binary World

C programming Language
Chapter 1:
Types, Operators
and Expressions
04 ‫ספטמבר‬
Copyright Meir Kalech
1
The Binary World
 Describes the world using only 0 and 1:
 open – 1, close – 0
 success – 1, failure – 0
 true – 1, false – 0



This dual notation representation is called
a “bit” (short for “BInary digiT”).
We can’t describe the door’s color or the
course number with 0/1.
Do you have any idea how to do that?
04 ‫ספטמבר‬
Copyright Meir Kalech
2
The Binary World
Great!!!
 Join two bits together.
 With two bits we can represent 4 states:





00
01
10
11
0
1
2
3
Now we can count 4 colors or 4 numbers.
But the raised question still remains.
04 ‫ספטמבר‬
Copyright Meir Kalech
3
The Binary World
JOINING AGAIN?!
 Join eight bits together – call them a “byte”.
 In eight bits we can represent 256 (28) states:





00000000
00000001
…
11111110
11111111
0
1
…
254
255
 But for describing the number of (real ) bugs
at my home, this byte is not large enough.
04 ‫ספטמבר‬
Copyright Meir Kalech
4
The Binary World
YES, JOIN THEM AGAIN!



But now join two bytes (not bits) together.
Our world contains now 216 states, and if it
is still not sufficient, join in two more bytes,
and then four, etc…
This scheme is OK for positive numbers.
But how can we represent negative numbers
or floating point numbers?
04 ‫ספטמבר‬
Copyright Meir Kalech
5
Negative Numbers

Negative numbers:

Assign the Most Significant Bit (MSB) as the sign
of the number, and use the remaining bits to represent
the number itself.
Each number could be formed in one of 2 notations:
1. Normal binary notation.
2. 2’s complement notation.
 In 2’s complement notation:
 If the MSB is 0 then the number is positive.
 If the MSB is 1 then the number is negative.

04 ‫ספטמבר‬
Copyright Meir Kalech
6
Negative Numbers

The following byte represents a certain
number. Which number is it?
1 0 0 0 0 1 0 1

There are 2 options here:
1. If the number is kept in the normal binary
notation, it is 133.
2. If the number is kept in the 2's complement
notation, it is -123.

How do I know this?
04 ‫ספטמבר‬
Copyright Meir Kalech
7
Negative Numbers
1. Let sum the bits from the MSB to LSB in the
following form:
1*27+0*26+0*25+0*24+0*23+1*22+0*21+1*20 = 133
2. If the number is kept in the 2's complement
notation, we complement and add 1 to get its
complement value:
1 0 0 0 0 1 0
not 0 1 1 1 1 0 1
+
0 1 11 1 0 1
04 ‫ספטמבר‬
Copyright Meir Kalech
1
0
1
1
(123)
8
Negative Numbers



What is the scope of each of the above
notations?
Normal binary notation:
00000000 to 11111111 = ((20-1) – (28-1))
2's complement notation:
There is no difference between positive 0 or negative
0. So the (asymmetric) scope is:
00000000 to 01111111 = ((20-1) – (27-1)) and
10000000 to 11111111 = (-(27) – -(20))
04 ‫ספטמבר‬
Copyright Meir Kalech
9
Floating Point Numbers

Floating-point representation involves
composing the number from 4 parts:
1.
2.
3.
4.

The
The
The
The
sign of the number.
mantissa ( 0 < mantissa < 1 ).
sign of the exponent.
exponent.
Examples:

23.456
The number’s sign is +
The mantissa is 0.23456
The exponent’s sign is +
The exponent is 2
04 ‫ספטמבר‬
-0.00078
The number’s sign is The mantissa is 0.78
The exponent’s sign is The exponent is 3
Copyright Meir Kalech
10
Floating Point Numbers


The figure below describes the general principle
of floating-point representation (details might
vary between different operating systems).
One way to represent floating point is:
1 bit
sign
23 bits
mantissa
1 bit
sign
7 bits
exponent
In this (single float) case, 32 bits (4 bytes)
are used.
04 ‫ספטמבר‬
Copyright Meir Kalech
11
Programmer’s Responsibility

It’s the programmer’s responsibility to know:



how many bytes are needed for the correct
representation of the number.
which data type is needed for this representation.
Examples:


If the program gets a student’s grade as input from the
user, the programmer needs to represent a positive
number between 0 to 100. One byte is enough for this.
If the program gets a customer account status from
the user, say of values between -1025308 to 250,
the programmer can use a floating point number.
4 bytes should be enough.
04 ‫ספטמבר‬
Copyright Meir Kalech
12
How to request this?
How could we define to the program
the requested number of bytes and
data type?
 The program uses keywords.
 Each keyword has a role in the
program environment.
 The program has keywords for the
pre-defined types.

04 ‫ספטמבר‬
Copyright Meir Kalech
13
Common (C) Types
unsigned char 1 byte
char
1 byte
unsigned short int - 2 bytes
short
2 bytes
unsigned int 2/4 bytes
int
2/4 bytes
unsigned long 4 bytes
long
4 bytes
float
4 bytes
double
8 bytes
long double
10 bytes
04 ‫ספטמבר‬
for characters
for characters
for short positive integers
for short integers
for positive integers
for integers
for large positive integers
for large integers
for floating point
for long floating point
for longer floating point
Copyright Meir Kalech
14
Characters


char and unsigned char are set to represent
characters.
Each character has two representations:
1. Binary
2. ASCII



The binary representation is a 8-bit number.
The ASCII representation is the “figure” of the
character.
An ASCII table can map between these two
representations.
04 ‫ספטמבר‬
Copyright Meir Kalech
15
Characters

For instance:

For the lower-case character ‘a’:
• Binary: 01100001
• ASCII: ‘a’

(97)
What is the difference between the number 3
and the character ‘3’?

The number 3 is:
• Binary: 00000011
(3)
• ASCII: end of text (ETX) (also the ^C character)

The character ‘3’ is:
• Binary: 00110011
• ASCII: ‘3’
04 ‫ספטמבר‬
(51)
Copyright Meir Kalech
16
Variables

A variable defines an area for storing data. Each has:





name
type
address
value
Examples:
100
‘a’



150
98587
char color
int catalog_number
Variable definition:
type variable_name;
 For instance:
int grade;
Name, type and address are not allowed to change.
If value hasn’t been defined/assigned, the variable value
is garbage.
04 ‫ספטמבר‬
Copyright Meir Kalech
17
Memory




Memory is an area in the computer that is used
to store variables.
The compiler is responsible for preparing an
execution file and to inform how many bytes in
memory are necessary for the execution of the
file.
The number of bytes needed is also derived from
the variables that are defined in the program.
Conclusion:
variable definition = needed memory area
04 ‫ספטמבר‬
Copyright Meir Kalech
18
#include - File Inclusion





The C language has portability and reusability
characteristics.
Portability: the language is not tied down to a
certain platform/environment.
Reusability: the language enables reuse of code.
The pre-processor directive “#include” enables
including (standard) utility files and user defined
files into the program during compilation.
This way, the programmer can use utilities that
were defined already by other programmers.
04 ‫ספטמבר‬
Copyright Meir Kalech
19
Input/Output (I/O)





C supplies functions that are responsible for input/output.
scanf is usually used for input and printf for output.
Syntax:
 scanf(“% format string”, &variable name);
 printf(“% format string”, variable name);
#include <stdio.h> library to use the I/O functions.
Example:
#include <stdio.h>
int x;
float y;
char z;
scanf(“%d %f %c”, &x, &y, &z);
printf(“x=%d y=%f z=%c\n”, x, y, z);
04 ‫ספטמבר‬
Copyright Meir Kalech
20
Operators
In addition to types, C also defines
operators.
 There are various classes of operators:

Assignment operators
 Arithmetic operators
 Increment/Decrement operators
 Relational operators
 Logical operators

04 ‫ספטמבר‬
Copyright Meir Kalech
21
Assignment Operators



Do you like mathematics? If not, CONGRATULATIONS!!!
The meaning of the assignment operator in C is definitely
different from its meaning in mathematics!!!
The assignment operator ‘=‘ assigns a given value
to a variable.
For instance:



int x = 5; // assigns 5 into the variable x.
x = x+2; /* assigns the current value of x (5)
plus 2 into x. Its new value is 7. */
Be careful of:
 overflow (rvalue is above the range of lvalue).
 underflow (rvalue is under the range of lvalue).
04 ‫ספטמבר‬
Copyright Meir Kalech
22
Arithmetic Operators





+
*
/
%

The precedence of the *, / and % operators, is
higher than that of the binary + and - operators.
The statement:
x = x + 2;
can be written in the form
x += 2;


plus (unary and binary)
minus (unary and binary)
multiplication
division
remainder of division (modulus)
The syntax of this assignment operation is:
• operand1 operator= operand2

Which is identical to:
• operand1 = operand1 operator operand2
04 ‫ספטמבר‬
Copyright Meir Kalech
23
Expressions




An expression is a combination of operators and
operands.
An operand may be a variable or constant.
The simplest expression is an operand by itself.
For instance:





6
2+3
7>1
a
a = a * (4 > 5)
04 ‫ספטמבר‬
Copyright Meir Kalech
24
Expressions

Each expression has:





Type
Value
Address
Type and value of the expression are derived from the
operands’ type and value.
For instance:



5
type is int, because the default of constant integers is
int; value is 5.
3.2 type is double, because the default of constant floating
point numbers is double; value is 3.2.
3+2 type is int because 3 is int and 2 is int; value is 5.
04 ‫ספטמבר‬
Copyright Meir Kalech
25
Expressions





And what about 3+2.4?
3 is int and 2.4 is double. What is the type of an
expression of int+double?
RULE: the operands in an expression should be
of the same types.
WHAT??? My grade in “introduction to computing” is
60 and my exercise grade is 59.5. Should I attend
the course again?
First, we should understand what is conversion.
If you understand, you will not have to take the
course again. But if not…
04 ‫ספטמבר‬
Copyright Meir Kalech
26
Conversions
Two types of conversions:
1. Implicit Conversion:
Invoked automatically by the compiler when an
expression mixes different types.
2. Explicit Conversion (Type Casting):
Invoked by the programmer code.

Conversion can result in:




Promotion: The type is converted to a “higher” type.
Demotion: The type is converted to a “lower” type,
which may lead to trouble (information loss).
A temporal variable is created during the
conversion process, that is identical to the original
variable to be converted, except its type.
04 ‫ספטמבר‬
Copyright Meir Kalech
27
Conversions

Comments on the conversion process:






The original variable does not change.
The temporal variable disappears at the statement end.
Example of implicit conversion:
double d = 12.5;
int a = 3, b = 4;
a = b + d;
Promotion to make this addition possible:
 b
int  double
Addition of the 2 double types:
 b + d
Demotion upon this assignment:
 b + d
double  int
(CAUTION!)
04 ‫ספטמבר‬
Copyright Meir Kalech
28
Conversions

Example of explicit conversion:
int a = 7, b = 4;
int result;
result = (float) a / b;



Promotion to make division possible (explicit)
 a
int  float
Promotion to make division possible (implicit)
 b
int  float
Demotion upon assignment
 a / b
float  int
(CAUTION!)
04 ‫ספטמבר‬
Copyright Meir Kalech
29
Increment/Decrement


C provides two (unusual) unary operators for
incrementing and decrementing variables by one (1).
Prefix Increment/Decrement operators: ++, -unary-expression:
++ unary-expression
-- unary-expression

Postfix Increment/Decrement operators: ++, -postfix-expression:
postfix-expression ++
postfix-expression --
04 ‫ספטמבר‬
Copyright Meir Kalech
30
Increment/Decrement



Prefix operator: the operand is incremented or
decremented by 1 and its new value is the value
of the expression.
Postfix operator: expression value is the value
of the postfix-expression before the
increment or decrement operator is applied.
Examples:
int x = 5, y;
 y = ++x;
 y = x++;
04 ‫ספטמבר‬
// y and x are 6
// y is 5, x is 6
Copyright Meir Kalech
31
Relational Operators








==
!=
>
<
>=
<=
Equal
Different (not equal)
Greater than
Less than
Greater or equal than
Less or equal than
== and != have a lower precedence than the
others, and all of them have a lower precedence
than the arithmetic operators.
If the relation is:


true: the relation returns 1.
false: the relation returns 0.
04 ‫ספטמבר‬
Copyright Meir Kalech
32
Relational Operators

Two examples:
int x=2;
(x>5)+1;
 The value of the expression x>5 is false (0).
 The value of entire expression is 1 (0+1).
int a=2, b;
((b = 2) == a);
 Expression value is true.
04 ‫ספטמבר‬
Copyright Meir Kalech
33
Logical Operators




Operators that can form more complex expressions.
Let a and b be expressions:
a
b
a || b a && b
!a
a || b
a or b
T
T
T
T
F
a && b
a and b
T
F
T
F
F
!a
not a
F
T
T
F
T
F
F
F
F
T
If the first (left) operand of a logical AND operation is
equal to 0, the second (right) operand is not evaluated.
If the first (left) operand of a logical OR operation is equal
to 1, the second (right) operand is not evaluated.
04 ‫ספטמבר‬
Copyright Meir Kalech
34
Logical Operators



Example: int a=5, b=3, c=0;
Which of the following expressions are TRUE (T)?
Which are FALSE (F)?
Expression
Evaluated as
Intermediate
Value (T or F)
a>b || c>b
5>3 || 0>3
T || F
T
a>b && c>b
a!=2 || !c
b<a || b<c && a<c
(b<a || b<c) && a<c
a || b && c
b<=a && (c=1)
04 ‫ספטמבר‬
Copyright Meir Kalech
35