Reflection: Data types
After researching about data types, I have learn somethings.
I, Summary
- Data type definition and how to use them.
- C has 4 primitive data types: int, char, float, double.
- How computers store negative integers.
- Data type definition and how to use them.
- C has 4 primitive data types: int, char, float, double.
- How computers store negative integers.
II, Applied to solve problems
There are some question from my teacher to check my knowledge.
There are some question from my teacher to check my knowledge.
- How to represent negative integer?
There are 3 ways to computers store negative integers using encoding schemes: two's complement notation, one's complement notation, and sign magnitude notation.
- Two's Complement Representation:
The most significant (leftmost) bit indicates the sign of the integer; therefore it is sometimes called the sign bit. If the sign bit is zero, then the number is greater than or equal to zero, or positive. If the sign bit is one, then the number is less than zero, or negative. To calculate the 2's complement of an integer, invert the binary equivalent of the number by changing all of the ones to zeroes and all of the zeroes to ones (also called 1's complement), and then add one.For example: 17 ==> -17 ----------------------------------------------- 0001 0001 --------------------Flip bit------------------- NOT(0001 0001) = 1110 1110 -------------------Add one------------------ 1110 1110 + 0000 0001 = 1110 1111 ------------------------------------------------ 1110 1111The ones' complement form of a negative binary number is the complement of its positive counterpart, which can be obtained by applying the NOT to the positive counterpart. Example: the ones' complement of 00101011 (43) 11010100 (−43). - Sign-Magnitude Representation The most significant (leftmost) bit indicates the sign of the integer; therefore it is sometimes called the sign bit. If the sign bit is zero, then the number is greater than or equal to zero, or positive. If the sign bit is one, then the number is less than zero, or negative. Example,
- Ones' complement:References: Introduction to Computer Science - Why int and float have the same size in 4 byte but float has range that larger than int many times They are totally different - typically int is just a straight forward 2's complement signed integer, while float is a single precision floating point representation with 23 bits of mantissa, 8 bits exponent and 1 bit sign, based on IEEE 754.
- Write program to display your profile on the screen
- Write demo program to input and output variables of different data types
III, Some of my thoughts
I have learned about 4 primitive data types of C: int, float, double, char. I have known how to convert between two data types. Besides, I also get more knowledge about how computer store negative integers and why people choose 2's Complement Representation to store integer.
Comments
Post a Comment