340 likes | 355 Views
INC 161 , CPE 100 Computer Programming. Lecture 2 Data Type. Topics. Expression Operation Flow Control if for while Function. Data type: int float char Array Pointer Structure. Command. 001101110101. Input. 0011010101 0101010000 1001010101 0101010001 0101010110
E N D
INC 161 , CPE 100Computer Programming Lecture 2 Data Type
Topics • Expression • Operation • Flow Control if for while • Function • Data type: int float char • Array • Pointer • Structure Command 001101110101 Input 0011010101 0101010000 1001010101 0101010001 0101010110 0010101000 Output CPU 001101110101 010101011000
Inside Memory Picture 111101111101 110101011011 Command 001101110101 010101011000 Word (Text) 00111111 00010011 Number 00110000 01010111 Cache, Ram, HDD
Number Format • Integer or Floating point • Signed or Unsigned • Size (8-bit, 16-bit, 32-bit, 64-bit)
Counting System We have 10 Symbols. (called Decimal system) 0 1 2 3 4 5 6 7 8 9 What if we want to represent a higher quantity?
We add another symbol at the front and recount. 8 9 0 1 11 12 Same principle applied for higher number. 98 99 100 101
Binary, Octal, Hexadecimal, and Decimal • Binary Binary numbering system has only two possible values for each digit: 0 and 1. binary number decimal number 0 0 1 1 10 2 11 3 100 4 101 5 110 6 1100 1010 202 Two Symbol 0 1
Decimal Numbers (base-10) • Decimal The digits' weight increases by powers of 10. The weighted values for each position is determined as follows: For example, A decimal number 4261 can be thought of as follows. 4 * 1000 + 2 * 100 + 6 * 10 + 1 * 1 = 4000 + 200 + 60 + 1 = 4261 (decimal)
Binary (base-2) • Binary The digits' weight increases by powers of 2. The weighted values for each position is determined as follows: For example, binary 10 is decimal 2. the binary value 1100 1010 represents the decimal value 202. 1 * 128 + 1 * 64 + 0 * 32 + 0 * 16 + 1 * 8 + 0 * 4 + 1 * 2 + 0 * 1 = 128 + 64 + 0 + 0 + 8 + 0 + 2 + 0 = 202 (decimal)
How to convert Decimal to Binary 55 27 remainder 1 13 remainder 1 6 remainder 1 3 remainder 0 1 remainder 1 Therefore, 55 = 110111
How about minus number? Use another bit call “sign bit” in the front. 0 = positive 1 = negative However, 0000 and 1000 will have equal value? In order to eliminate conflict, when make comparison, numbers representation should not be repeated. Two-complement system is defined.
Binary Two’s Complement System This system is used to represent negative numbers. The left-most bit is the sign bit. If it is 1, then the number is negative. Otherwise, it is positive. Give a negative value, represent it in binary two’s Procedure to change from positive to negative no. • write the number in its absolute value. • complement the binary number. (1st complement) • plus 1. (2nd complement)
Binary Two’s Complement System Example, represent –2 in binary two’s complement with 16 bits • Binary value of 2: 0000 0000 0000 0010 • Binary complement of 2: 1111 1111 1111 1101 • Plus 1: +1 • Two’s complement of -2: 1111 1111 1111 1110
Example 3-bit two complement 100 -4 101 -3 110 -2 111 -1 000 0 001 1 010 2 011 3
Relationship of Base 2 to higher Binary (base-2) Base-4 Octal (base-8) Hexadecimal (base-16) Can be easily converted between each other.
Octal The octal system is based on the binary system with a 3-bit boundary. The octal number system uses base 8 includes 0 through 7. The weighted values for each position is as follows: • Binary to Octal Conversion • Break the binary number into 3-bit sections from the least significant bit (LSB) to the most significant bit (MSB). • Convert the 3-bit binary number to its octal equivalent. For example, the binary value1 010 000 111 101 110 100 011 equals to octal value (12075643)8.
Octal to Binary Conversion • Convert the octal number to its 3-bit binary equivalent. • Combine all the 3-bit sections. For example, the octal value45761023 equals to binary value 100 101 111 110 001 000 010 011. • Octal to Decimal Conversion To convert octal number to decimal number, multiply the value in each position by its octal weight and add each value together. For example, the octal value (167)8 represents decimal value 119. 1*64 + 6*8 + 7*1 = 119
Hexadecimal Similar to octal, the hexadecimal system is also based on the binary system but using 4-bit boundary. The hexadecimal number system uses base 16 including the digits 0 through 9 and the letters A, B, C, D, E, and F. The letters A through F represent the decimal numbers 10 through 15. For the decimal values from 0 to 15, the corresponding hexadecimal values are listed below. Decimal Hexadecimal
The weighted values for each position is as follows: The conversion between binary value and hexadecimal value is similar to octal number,but using four-bit sections. 1001 1111 0101 1010 0110 = 9F5A6 The hexadecimal value 20A represents decimal value 522. 2*256 + 0*16 + 10*1 = 522
How to store floating-point? Exp is an integer. Suppose we use 3 bit to store the mantissa 000 001 010 011 100 101 110 111 0.000 0.125 0.250 0.375 0.500 0.625 0.750 0.875
Inside Memory Picture 111101111101 110101011011 Command 001101110101 010101011000 Word (Text) 00111111 00010011 Number 00110000 01010111 Cache, Ram, HDD
How we represent sentence? A sentence comes from several words A word comes from several characters Therefore, let’s define a code for each character
Character Set The character set in C includes the following members: • the 26 uppercase letters of the Latin alphabet A B C D E F G H I J K L MN O P Q R S T U V W X Y Z • the 26 lowercase letters of the Latin alphabet a b c d e f g h i j k l mn o p q r s t u v w x y z • the 10 decimal digits 0 1 2 3 4 5 6 7 8 9 • the following 31 graphic characters ! " # % & ' ( ) * + , - . / : ; < = > ? [ \ ] ^ _ { | } ~ $ ` We use code for each letter called ASCII code. This is a 8-bit code.
Integer Constants • An integer can be specified in decimal, octal, or hexadecimal. • A leading 0(zero) on an integer constant indicates an octal integer. • A leading 0x or 0X indicates a hexadecimal integer. Example: 30 (decimal) = 036 (octal) = 0X1e or 0x1E (hexadecimal)
Character Constants A character constant, stored as an integer, can be written as one character within a pair of single quotes like ‘x’. A character constant can be assigned to the variable of type char. For example, > char c = ’x’ > c x
Initialization • The declaration of a variable may be accompanied by an initializer that specifies the value of the variable should have at the beginning of its lifetime. • The initializer for a scalar shall be a single expression, such as > int i = 6/3 > double d = 12.345 > char c = ’a’
Operator & Expression • Operator: =, + ,- , < , etc. • Expression: 12, a+b, a-3, etc.
The Assignment Operator = • The regular assignment operator ‘=‘. • It will calculate the right side of = and assign the right variable to the calculated value. • When a floating-point number is assigned to an integer variable, the fractional part will be discarded. > int i; > float d = 10.789; > i = d; (i = 10) > i = d + 0.5; (i = 11)
Arithmetic Operators • There are five arithmetic operators: • + Addition • - Subtraction • * Multiplication • / Division • % Modulus • The multiplication operator * is needed for multiplication. • The result of the % operator is the remainder. If the value of the second operand is zero, the behavior is undefined. • The operands of the % operator shall have integer type.
> int i = 10; > i = 5*i; (i = 50) > i = i / 11; (i = 4) > i = 50 % 11; (i = 6)
More Data Type Example from a 32-bit OS char - 8 bit short – 16 bit int – 32 bit float – 32 bit double – 64 bit long double – 128 bit Low order High order
The algorithms and resultant data types of operations depend on the data types of the operand. • For binary operations, such as addition, subtraction, multiplication, and division, the resultant data type will take the higher order data type of two operands. • For example, the addition of two float numbers will result in a float, while the addition of a float and a double will result in a double. 2 / 3 int / int -> int 0 2.0 / 3 float / int -> float 0.667