580 likes | 1.76k Views
Binary Number System. Digital electronic devices use the binary number system for their internal working The decimal number system has 10 numbers: 0,1,2,3,4,5,6,7,8,9 Binary number has only two possible numbers : 0 and 1 Counting in binary goes as: 0, 1, 10, 11, 100, 101 and so on.
E N D
Binary Number System • Digital electronic devices use the binary number system for their internal working • The decimal number system has 10 numbers: 0,1,2,3,4,5,6,7,8,9 • Binary number has only two possible numbers : 0 and 1 • Counting in binary goes as: 0, 1, 10, 11, 100, 101 and so on
Decimal to Binary Conversion • Short division by 2 with remainder method • E.g. convert 1810 to binary 2 18 2 9 0 2 4 1 2 2 0 2 1 0 Binary equivalent of 18 is 10010
Binary to Decimal Conversion • Convert 10010 to decimal • 4 3 2 1 0 position • 1 0 0 1 0 number (24) + (21) = 16 + 2 = 18 If a binary number is unsigned then its max decimal value is 2n – 1 If a binary number is signed then its max decimal value is 2n-1 – 1 and Its min value is -2n-1
Basic data types • There are only a few basic data types in C • char: a single byte, capable of holding one character e.g. the letter ‘a’ • int: an integer of fixed length, typically reflecting the natural size of integers on the host machine (i.e., 32 or 64 bits). E.g. the number 10 • float: single-precision floating point. e.g. the number 10.67 • double: double precision floating point
Qualifiers for Basic data types • There are qualifiers which can be applied to the basic types • length of data • short int: • "shorter" int, <= number of bits in an int, • can also just write "short“ • long int: • a "longer int", >= number of bits in an int, often the same number of bits as an int • can also just write "long • long double: generally extended precision floating point • signed and unsigned • unsigned int: • an int type with no sign, if int has 32-bits, range from 0..232-1 • also works with long and short • unsigned char: a number from 0 to 255 • signed char: a number from –128 to 127 (8-bit signed value)
C Basic data types – size On A typical 32-bit machine Type Keyword Bytes • Character char 1 • integer int 4 • short integer short 2 • long integer long 4 • long long integer long long 8 • unsigned integer unsigned int 4 • unsigned short integer unsigned short 2 • unsigned long integer unsigned long 4 • single-precision float 4 • double-precision double 8 • Long double long double 12
The sizeof() operator / function The sizeof() function returns the number of bytes in a data type. intmain() { printf("Size of char ......... = %d byte(s)\n", sizeof(char)); printf("Size of short ........ = %d byte(s)\n", sizeof(short)); printf("Size of int ........... = %d byte(s)\n", sizeof(int)); }
Type casting (Explicit) • C allows for conversions between the basic types, implicitly or explicitly. • Explicit conversion uses the cast operator. • Example: • int x=10; • float y, z=3.14; • y=(float) x; /* y=10.0 */ • x=(int) z; /* x=3 */ • x=(int) (-z); /* x= -3 rounded approaching zero */
Type casting (Implicit) • Implicit conversion is done by the compiler when it expects one type at a position, but another type is provided. • Conversion during assignments: • char c='a'; • int i; • i=c; /* i is assigned the ASCII code of ‘a’ */ • Arithmetic conversion – when operands of a binary operator are not the same type: • int i=5 , j=1; • float x=1.0 , y; • y = x / i; /* y = 1.0 / 5.0 */ • y = j / i; /* y = 1 / 5 so y = 0 */ • y = (float) j / i; /* y = 1.0 / 5 */ /* The cast operator has a higher precedence than division*/
Operators and Expressions • The symbols which are used to perform logical and mathematical operations in a program are called operators,e.g. +, -, *, / • Operators, functions, constants and variables are combined together to form expressions. • A + B * 5 is an expression • C = A + B * 5; is an assignment statement
Operators and Expressions • C language provides the following types of operators: • Arithmetic Operators • Relational Operators • Logical Operators • Bitwise Operators • Assignment Operators
Arithmetic Operators • Assume variable A holds 10 and variable B holds 20 then:
Practice • Convert total days into weeks and days • Assume total days = X, • Weeks = X / 7; • Days = X % 7;
Relational Operators* • Assume variable A holds 10 and variable B holds 20 then: * Work with arithmetic and string types
Practice • Assume Orlando’s population = 1000000 and • LA’s population = 5000000 • Use relational operators to compare the two populations
Logical Operators • Assume variable A holds 1 and variable B holds 0 then:
Practice • Assume 0 means false and 1 means true (in C any non-zero value is treated as true) • Let apples_are_square = 0; • And oranges_are_round = 1; • Try logical operators on these variables
Conditional Operator • A ternary operator. • Consist of two symbols: the question mark (?) and the colon (:). • Syntax: • Identifier = (test expression)? Expression1: Expression2 ; • It is not a statement, therefore represents a value. • The operator works by evaluating test expression. • If it is true (non-zero), it evaluates and returns expression1. Otherwise, it evaluates and returns expression2.
Practice for Conditional Operator: • Calculate average profit per customer for a given day, with the possibility of having no customer at all (May be because of a holiday).
Operator Precedence • Consider the evaluation of following expressions: • 3 + 7 – 1 • 8 * 3 / 6 • We usually go from left to right for solving these expressions • Now consider the following expression: • 10 - 4 * 2 • If evaluated from left to right then the answer would be: 12 • In arithmetic, multiplication and division have higher precedence than addition and subtraction. • So, the result of above statement would be 2, and NOT 12
Operator Precedence • Now consider the following expression: • 3 + 2*4 - 8/2*4/8 + 3*6 - 4*2*2 • Go from left to right but solve the / and * first: • 3+8-2+18-16 • Now solve the + and –, you get: • 11 • To enforce some other order of precedence, we can use parentheses • Consider the same expression with parentheses: • (3 + 2)*4 - 8/2*4/8 + 3*(6 – 4)*2*2, we get: • 5*4 - 8/2*4/8 + 3*2*2*2 • 20-2+24 • 42 • For the time being, we will consider the precedence of only these three levels: (), */ and +-