CS156: Bits, Bytes, and Numbers - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

CS156: Bits, Bytes, and Numbers

Description:

Numbers. 2. The Bit. A bit is the fundamental unit of ... We can print numbers in octal and hex using the formatting strings %o and %x, respectively. ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 25
Provided by: csColo
Category:
Tags: bits | bytes | cs156 | numbers

less

Transcript and Presenter's Notes

Title: CS156: Bits, Bytes, and Numbers


1
CS156 Bits, Bytes, and Numbers
  • Lecture

2
The Bit
  • A bit is the fundamental unit of information in
    the digital realm.
  • A bit has two values 0/1, false/true, off/on
  • Other units are made up of bits
  • Byte 8 bits
  • Kilobyte (kB) 210 Bytes 1024 Bytes
  • Megabyte (MB) 220 Bytes
  • Gigabyte (GB) 230 Bytes 210 MB

3
Combinatorics of the Bit
  • With one bit we can represent two values, 0 or 1.
  • With sequences of bits we can represent
    (exponentially) more values.
  • A sequence of n bits can take on 2n different
    values.
  • For n3, we have 238
  • different values.

000 011 110 001 100 111 010 101
4
Binary Number System
  • The binary number system is a way of representing
    numbers with a sequence of bits.
  • dn dn-1 d1 d0.d-1 d-2
  • Each d is a binary digit.
  • The value of a number is computed as

5
Binary Number Example
  • What is the value of 1001001.01 in decimal
    format?
  • 26 23 20 2-2 64 8 1 1/4
  • 73.25
  • While binary data is easy for a computer to read
    it is a bit tedious for humans.

6
Other Number Systems
  • Other systems exist for different bases.
  • Octal Base 8, 0-7
  • Decimal Base 10, 0-9
  • Hexadecimal Base 16, 0-9 and A-F (10-15)
  • Example
  • Binary 1101100
  • Octal 154 1 101 100 ? 1 5 4
  • Decimal 108
  • Hexadecimal 6C 0110 1100 ? 6 C

7
C Numeric Literals
  • The C Language allows integer literals to be
    specified in decimal, octal, and hex.
  • Octal numbers start with 0 (zero). 010 is eight!
  • Hex numbers start with 0x.
  • We can print numbers in octal and hex using the
    formatting strings o and x, respectively.

// assigning the value 10 in different
formats int my_number 10 int my_octal
012 int my_hex 0xA
8
Representing Integers
  • Integers can be represented in a number of
    different ways in a computer. Given a fixed
    number of bits to represent the number we are
    limited in how many integers we can represent.
  • If we wish to allow positive and negative
    numbers, half of the possible values are assigned
    to positive numbers and half to negative numbers.
  • For example, if an integer is represented with 8
    bits, we can represent 28 numbers, ranging from
  • -128 to 127.

9
Integer Types in C
  • We have seen two basic integer types so far, int
    and char. C has some modifiers for using
    different types of integers.
  • The three modifiers are
  • short use a representation with less (or equal)
    number of bits than the unmodified type
  • long use a representation with more (or equal)
    number of bits than the unmodified type
  • unsigned use an unsigned representation
  • 0 to 2n-1

10
Integer Types in C
11
Example
  • We can use sizeof to see the size of various
    types on a given machine/compiler.Your sizes may
    vary!

int main() printf(Size of short int
d\n,sizeof(short int)) printf(Size of int
d\n,sizeof(int)) printf(Size of long int
d\n,sizeof(long int)) printf(Size of char
d\n,sizeof(char)) return 0
cs156 gt gcc size.c cs156 gt ./a.out Size of short
int 2 Size of int 4 Size of long int 4 Size of
char 1
12
Overflow
  • What happens if we try to store a integer number
    that is too big for the given type?

int main() short int my_short_int 8000 int
my_int 8000 my_short_int
my_int printf(The result d\n,my_short_int)
return 0
cs156 gt ./a.out my_short_int 8000 my_int
8000 Result -28672
13
Overflow
  • The result was too big for the type we used so we
    got a value we did not expect.
  • This error is known as overflow because the
    number became too big for the type.
  • A similar error can occur if we try to subtract a
    number from an unsigned int with value 0.

14
Real Numbers
  • C allows us to represent real numbers in floating
    point format. Like integer numbers floating
    point numbers in C have a fixed range.
  • d.ddddd x 10e
  • The digits d.ddddd are known as the significand,
    or mantissa, and the e is the exponent.

15
Real Numbers
  • When representing floating point numbers in a
    computer we have to decide several things.
  • How many bits do we use for the significand?
  • How many bits do we use for the exponent?
  • Should we have special values for /- infinity
    and NaN?
  • One standard format used by most modern computers
    is the IEEE 754 floating point standard.
  • Floating point arithmetic in a computer
    introduces a number of problems.

16
Floating Point and Precision
  • Consider a floating point representation that
    uses four decimal digits for the significand and
    has an exponent range of /- 4.
  • Some numbers we can represent
  • 43210.0 4.321 x 104
  • 4.321 4.321 x 100
  • .004321 4.321 x 10-3
  • Some numbers we cant represent
  • 432.14 4.322 x 102 (432.2)
  • 43211.1 4.321 x 104 (43210.0)

17
Rounding Errors and Floating Point Numbers
  • Some numbers, like 0.2, can not be represented
    exactly in a binary floating point
    representation.
  • Some operations result in values that can not be
    represented exactly, 2/3 0.66667.
  • These types of errors are known as rounding
    errors and can cause computations to produce
    erroneous results.

18
Other Errors and Floating Point Numbers
  • Adding small numbers to large numbers results in
    absorption. 1 x 1020 1 1 x 1020
  • Attempting to represent numbers that are too
    large results in overflow. In our example we
    would get this error with 5.0 x 105.
  • Attempting to represent a number that is too
    small results in underflow. For example,
    0.00001234, 1.234 x 10-5.

19
Floating Point Arithmetic
  • Because of these kinds of errors, floating-point
    arithmetic does not have certain properties that
    we might expect.
  • associativity a(bc) ! (ab)c
  • distributivity a(bc) ! ab ac
  • To minimize the effects of these types of errors,
    we can modify our algorithms and/or use more bits
    to represent the numbers.

20
Floating Point Types in C
  • C provides two type specifiers for floating point
    numbers float and double.
  • The modifier long can be used on the double type.
  • The actual number of bytes used for these types
    will vary with compiler and machine.

21
Example
int main() printf(Size of float
d\n,sizeof(float)) printf(Size of double
d\n,sizeof(double)) printf(Size of long
double d\n,sizeof(long double)) return 0
cs156 gt gcc sizef.c cs156 gt ./a.out Size of
float 4 Size of double 8 Size of long double
12 cs156 gt gcc a.out cs156 gt ./a.out Size of
float 4 Size of double 8 Size of long double 16
22
Float versus Double
  • C requires that all numeric computations be
    evaluated in double precision.
  • So, all floats are converted to double before use
    in an expression and then converted back to float
    afterwards.
  • The speed of computation with float and double
    varies with machine type.

23
More printf
  • We can use the formatting symbol ld to print
    long int variables. The f can be used for
    doubles as well as floats (we learned why on the
    previous slide).
  • Using .ddd f in f (.3f) says how many digits of
    the floating point number to print.
  • Using e will print the number in
  • d.ddd E e format.

24
Example
int main() double d 10234.13202
printf("f \n 0.3f \n 0.6f \n e
\n",d,d,d,d) return 0
cs156 gt gcc printf_float.c cs156 gt ./a.out
10234.132020 10234.132 10234.132020
1.023413e04
Write a Comment
User Comments (0)
About PowerShow.com