Physics 306 Computing Lab 5: A Little Bit of This, A Little Bit of That

Size: px
Start display at page:

Download "Physics 306 Computing Lab 5: A Little Bit of This, A Little Bit of That"

Transcription

1 Physics 306 Computing Lab 5: A Little Bit of This, A Little Bit of That 1. Introduction You have seen situations in which the way numbers are stored in a computer affects a program. For example, in the first lab, you calculated n! for larger and larger n. When the numbers got very large, the computer started producing incorrect results numbers which were too small, and even negative numbers. These resulted from the way that the computer stores and manipulates numbers. Today you will explore how numbers are stored by the computer when running a C program. The technical standard for the C language does not specify how numbers are stored. This is traditionally a matter left to the designers of the hardware and operating system of an individual computer. In practice, most machines, and most computer languages, use formats and storage methods very similar to those you will see today. 2. Binary numbers The digital hardware inside a computer uses binary logic, zeros and ones. All numbers are stored as binary numbers and all arithmetic is be done in binary. Recall that, in base ten, a number like 8325 means (reading right to left), 5 ones plus 2 tens plus 3 hundreds plus 8 thousands. The columns (ones, tens, hundreds, thousands) are powers of ten. Similarly, in binary, the numerical columns are powers of two (ones, twos, fours, eights, sixteens, etc.), so that would be, reading right to left, 1 one, 1 two, 0 fours, 0 eights, and 1 sixteen, for a total of 19. Thus in binary is 19 in decimal. The individual digits in a binary number are called bits. If you get confused about binary numbers, ask for help. Enter and compile the following program. #include "stdio.h" int main() { int a; int i; printf("enter a:\n"); scanf("%d",&a); for (i=31; i>=0; i--) printf("%d", ((a>>i)&1) ); printf ("\n"); 1

2 There are two things in this program which may be new to you. First, the loop runs backwards. It starts with i at 31, decreases it by 1 in each iteration of the loop, (that s what i-- does), and runs as long as i 0. The second new thing is the expression ((a>>i)&1) This is an expression which manipulates the individual bits of numbers. We will discuss it later. For now, just run the program. Enter a number and the program will print it out as a binary number. Try a few numbers and convince yourself that it works. Notice that it prints out 32 bits. That is the number of binary digits in which an int variable is stored. In fact, only the rightmost 31 bits are used to store the size of the number. The leftmost bit, called the sign bit, is 0 if the number is positive (or zero) and 1 if the number is negative. The story is a little more complicated than that. Try entering a negative number into the program. What happens? Hmmm, strange. The binary representation of 1 is The binary representation of 2 is The binary representation of 3 is , and so on. Arithmetic within the negative numbers works as you would expect. For example, = 2: But why does 1 expressed in binary have one in every column? There is a good reason for this. Think about what properties the computer representation of 1 should have. One important property of the number 1 is that when you add +1 to it, you get zero. With the choice of to represent 1, adding the binary patterns for 1 and +1 gives the following: (Do you see how that arithmetic works? There is a lot of carrying involved.) Notice that the result is thirty-three digits long. However, the computer only stores 32 bits. The computer does not keep track of the thirty-third digit (the new, leftmost one). This digit simply disappears. The remaining 32 bits are all zeros, which is exactly what we want. So, to the computer, the arithmetic looks like this: In other words, = 0, as expected. 2

3 This method for storing negative integers, which is used in all modern computers, is called two s complement representation. Here s a recipe for calculating a two s complement representation, using 4307 as an example. (i) Write out the binary pattern for (ii) Change all the 1s to 0s and all the 0s to 1s (this is called the one s complement of the number). (iii) Add 1 (to get the two s complement ) one s complement plus one How much memory? The usual units for talking about computer storage are not bits but, rather, bytes. One byte is eight bits. At some point in the past, eight bits was the typical amount of information a computer processor could handle at once. These days, thirty-two bits (four bytes) and sixty-four bits (eight bytes) are common. C can tell you how many bytes it takes to store a variable. #include "stdio.h"; int main() { int a, n; n = sizeof(a); printf ("An integer is stored as %d bytes\n",n); This prints the size of a in bytes. The C language allows integers which are longer or shorter than this. You might use a longer integer if you need more digits of precision. You might use a shorter integer if storage space is at a premium. The four types of integers allowed in C as implemented by gcc on our imacs are short, int, long, and long long : short a; int b; long c; long long d; Write a quick program which prints out the length of each of these types of variable. You will find that two of them are identical to each other. The only official rules in the C language are that short must be shorter than or equal to int, that int must be shorter than or equal to long, and so on. What about arrays? Define an array of, say, 100 integers: 3

4 int n[100]; What is the size of this array (use your program to find out)? Is it what you expect? What if you make an array of long long variables? 4. Bitwise operations Let s get back to the expression ((a>>i)&1), which was in your first program. This is an example of manipulating the bits of a number. First let s tackle a>>i. This means to take the number a and shift each bit to the right by i places. For example, a>>1 would shift each bit to the right by one place. Thus whatever was in the eights column would end up in the fours column, whatever was in the fours column would end up in the twos column, etc. a a>> This is exactly what you would get if you divided a by 2. The other operator in that expression, & is called bitwise and. A typical use is: c = a & b. This takes the two numbers a and b compares them bit by bit. If a given bit is 1 in both a and b then it is set to 1 in the output number (hence the name and ). If it is 0 in either of the input numbers, then it is set to 0 in the output number. a b c=a&b Write a program to explicitly demonstrate the bitwise and operator. Your program should have the user enter two numbers and calculate the bitwise-and combination of them. Write all three out as binary numbers, as in the above example. The most efficient way to write such a program is to put the binary-printing loop into a separate function. Let s talk about functions for a minute. You have written functions to calculate, say, 5(sin x exp x). Such a function has an input parameter, x, and returns a value, whatever number it calculates. Whenever you call f(a) in the main program, where a is some number, the computer jumps to the function, sets x to the value of a, runs whatever calculation is in the function, and then returns to the main program. Functions can do more than just calculate numbers. They can include any standard C commands (variable declarations, loops, writing output, etc.). They can return a value, but it is not always required to. (Some purists will point out that, technically, a function always returns a value. But if we don t set a return value, and we don t look for a return value, does it matter?) Here s a simple example of a function. Whatever value it is given, it prints ten times. That s a silly thing to do. This is just meant to show how functions work. It is not meant to be practical. 4

5 #include "stdio.h" int f(int x) { int i; for (i=0; i<10; i++) printf("%d\n",x); int main() { int a,b,c; printf("enter a b c separated by spaces: \n"); scanf("%d %d %d",&a,&b,&c); printf("i will now print each of these ten times:\n"); f(a); f(b); f(c); Calling a function is simple: just use a command like f(a);. understand how this program works before continuing on. Make sure you Getting back to the matter at hand, write a function which, when given an integer, writes it out in binary. This is tantamount to moving the loop in the first program in this lab into a separate function, along with the appropriate declarations. Then write a main program which has the user enter two numbers, a, and b, calculates the bitwise and, c = a&b, and writes out a, b, and c in binary form. (Confused? Get help!) Once that program is working, copy it and modify it to demonstrate the right-shift operator, i.e., to calculate c = a >> b. Read in some number, shift it by some number of digits, and print out the original number and the shifted number in binary. When a number is shifted to the right, the computer has to decide what to put in the leftmost digit (which is otherwise empty). Try shifting both positive and negative numbers, and look at what the computer puts in the leftmost digit in each case. Can you see why this might be useful? (Hint: in binary, shifting to the right by one place is equivalent to dividing by 2.) Now that you ve experimented with the shift and bitwise-and operators, take another look at the code you have been using. Do you understand how these lines print out a number in binary? for (i=31; i>=0; i--) printf("%d", ((a>>i)&1) ); 5

6 There are other bitwise operators. Left shift, a<<n, shifts bits to the left. Bitwise or, a b, sets each bit of the output to 1 if either that bit of a or that bit of b is 1. Bitwise exclusive or (sometimes called bitwise xor ), a^b, sets each bit of the output to 1 if either that bit of a or that bit of b is 1, but not if both are Floating point numbers When working with large or non-integer numbers, you have to use float variables. These are stored as patterns of bits, but in a different way than integers. What does a binary float number look like? Consider how we write decimal numbers in exponential form. The number is written We can express numbers similarly in binary, except that the digits are all 0 s and 1 s, and the exponential is 2 n instead of 10 n. Here s an example. The decimal number is in binary. Do you see why? The integer part, 26, is The fractional part, 0.125, is one eighth. With decimal numbers, the digits to the right of the decimal point are tenths, hundredths, thousandths, and so on. With binary numbers, the digits to the right of the decimal points are halves, quarters, eighths, and so on. Hence one eighth, in binary, is 0.001, and is In exponential notation, the binary number is The 4 in the exponent is because the decimal point is shifted over by four places. Notice that binary numbers written in exponential notation always have a one to the left of the decimal place. Think about it. Try a converting a few numbers into binary to convince yourself that this is true. Mathematica can convert numbers to binary; for example: BaseForm[26.125,2]. To ensure that the result is in exponential notation, write, for example, BaseForm[ScientificForm[26.125],2]. So, what does the binary number look like in the computer s memory? It has three parts: The sign of the number, 0 for positive numbers and 1 for negative numbers. The exponent, 4 in this example. Exponents are stored as positive integers. In order to allow for negative exponents, some fixed number (called the bias ) is added to the exponent before it is stored. On our computers, 8 bits are used to store the exponent part of float variables, so the stored exponent numbers run from 0 to 255 (since 255, which is in binary, is the largest possible 8-bit number). The bias is 127. So, in the example number, the exponent as stored in the computer memory is 4+127=131, which is in binary. The fractional part, This is called the mantissa. Since it always beings with a 1, there is no reason to actually store the 1 in computer memory. Just is the part after the decimal, is stored. In our computers, the stored part of the mantissa is 23 bits long. Numbers with fewer than 23 digits are padded with zeros on the right side; this gives in our example. 6

7 The bits are stored in the order just listed: first the sign (1 bit), then the exponent (8 bits), then the mantissa (23 bits). Thus the number stored in memory looks like: , where the three boxes store the sign, the exponent, and the mantissa. Of course, if you print out the number, you don t see boxes; it just looks like: Writing out the bit pattern of a floating point number takes a little bit of trickery. The problem is that the bit-shift operator and the bitwise-and operator operate on integers, but not floating point numbers. What you need to do is to store a floating point number in memory, and then trick the computer into thinking it is an integer, so that you can use the binary-number-printing function you wrote earlier. Here is how you can do it: float a; int b;... set a to some real number... b = *(int *)&a;... print out the binary pattern in integer b... How does b = *(int *)&a work? (Don t worry if you don t follow this.) The variable a holds the floating point number. The expression &a refers to the location in memory (the address ) where that number is stored. This is called a pointer. (Confusingly, when & operates on two variables, such as a & b, it is the bitwise-or operator, but when it operates on a single variable, &a it returns a pointer to that variable. These two uses of & have nothing whatsoever to do with one another.) The value returned by &a is, specifically, a pointer to a float. The operator (int *) turns it into a pointer to an integer. (These are different variable types in C although in practice the number (int *)&a is probably identically equal to &a.) Finally, the * on the far left follows the pointer to an integer and retrieves the value stored there, treating it as an integer. This integer has the same bit pattern as the original floating point number. The use and manipulation of pointers is an important part of programming in C, but we won t use pointers much in this course. Write a program to print out the bit patterns of floating point numbers. Try it with Try it with Try it with , which is divided by two. Try it with a few other positive and negative numbers and see if the bit patterns make sense. What is the largest number you can store? What happens if you try to do arithmetic which is invalid, such as a = 1./0. or sqrt( 1), or if you multiply two large numbers together so that the result cannot fit in a 32-bit float? Try it, writing the result of the arithmetic both in the normal way, printf("%f"), and as a bit pattern. Any floating-point number with exponent all ones ( ) represents either inf (infinity) or nan (not a number). Either of these means that an invalid arithmetic operation was attempted or that the result of an arithmetic operation was out of the range of float variables. 7

8 In addition to float variables, C has double and long double variables. On our computers, the format of doubles is the same as the format of floats, except that they have 11 bit exponents (with bias 1023) and 52 bit mantissas; along with the sign bit, this gives a total of 64 bits or 8 bytes. The format of long doubles is different. They are actually two doubles, stored one after another, one of which is much smaller than the other (by a factor of around 2 52 or ). The total value of the long double variable is the sum of the two doubles. 6. Character variables So far we have been considering how computers store numbers. What if we want to store a word, or a sentence? How does the computer store hello? In computer-speak, this is a string. In C, a string is an array of characters, so we had better talk about characters first. There is a C data type called char, which holds exactly one character: char a; a = A ; printf("%c\n",a); A string is an array of characters. For example: #include <stdio.h> int main() { char a[4]; a[0] = A ; a[1] = B ; a[2] = C ; a[3] = \0 ; printf ("%s\n",a); The last character, \0, means end of string. Strings normally end with this character so that commands which use the string, such printf(), know where the string ends. You don t always have to refer to a string one character at a time. For example, the following declares the array a, fills it with ABC, and adds a \0 at the end. When the size of the array is not specified inside the square brackets, C will make the array just large enough to hold whatever string is specified ( ABC\0 in this instance.) char a[] = "ABC"; You can read a string from the keyboard: char a[80]; scanf("%s",a); // notice: no ampersand before the variable name Perhaps surprisingly, the following does not work. 8

9 char a[80]; a = "Hello world!"; // Does not work This bit of code doesn t work because C is not able to manipulate the entire array of characters in a single statement. Instead, there if a function copies one string into another string. The following copies Hello world! into string a: char a[80]; strcpy(a,"hello world!"); Characters are one byte long. (Try sizeof(char).) The program below will read in a string and type out the 8-bit binary patterns used to store each character, along with their numerical values in base 10. Try it out. Can you find patterns, e.g., what 8-bit numbers are used to represent A, B, C, and so on? What 8-bit numbers are used to represent a, b, and c,? What about digits 1, 2, etc.? Can you figure out how the program works? #include <stdio.h> int printbyte(int x) { int i; for (i=7; i>=0; i--) printf ("%d",((x>>i)&1)); int main() { char a[80]; int b; int i; printf("enter a string:\n"); scanf("%s",&a); i = 0; do { b = a[i]; printbyte(b); printf(" %3d %c\n",b,a[i]); i++; while (b!=0); 9

10 .... The floating-point numbers you saw today are in a format called the IEEE standard for binary floating-pint arithmetic or IEEE 754. (The IEEE is the Institute of Electrical and Electronics Engineers, a professional organization for people in these fields.) The Wikipedia has a good article on IEEE 754 formats and arithmetic. The character variables you saw in section 6 are in a format called ASCII (American Standard Code for Information Interchange). You can find tables of ASCII codes on the internet. 10

Bits, Words, and Integers

Bits, Words, and Integers Computer Science 52 Bits, Words, and Integers Spring Semester, 2017 In this document, we look at how bits are organized into meaningful data. In particular, we will see the details of how integers are

More information

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example Appendix F Number Systems Binary Numbers Decimal notation represents numbers as powers of 10, for example 1729 1 103 7 102 2 101 9 100 decimal = + + + There is no particular reason for the choice of 10,

More information

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture 05 I/O statements Printf, Scanf Simple

More information

Logic, Words, and Integers

Logic, Words, and Integers Computer Science 52 Logic, Words, and Integers 1 Words and Data The basic unit of information in a computer is the bit; it is simply a quantity that takes one of two values, 0 or 1. A sequence of k bits

More information

Data Representation 1

Data Representation 1 1 Data Representation Outline Binary Numbers Adding Binary Numbers Negative Integers Other Operations with Binary Numbers Floating Point Numbers Character Representation Image Representation Sound Representation

More information

CS101 Lecture 04: Binary Arithmetic

CS101 Lecture 04: Binary Arithmetic CS101 Lecture 04: Binary Arithmetic Binary Number Addition Two s complement encoding Briefly: real number representation Aaron Stevens (azs@bu.edu) 25 January 2013 What You ll Learn Today Counting in binary

More information

COMP 122/L Lecture 2. Kyle Dewey

COMP 122/L Lecture 2. Kyle Dewey COMP 122/L Lecture 2 Kyle Dewey Outline Operations on binary values AND, OR, XOR, NOT Bit shifting (left, two forms of right) Addition Subtraction Twos complement Bitwise Operations Bitwise AND Similar

More information

Data Representation Type of Data Representation Integers Bits Unsigned 2 s Comp Excess 7 Excess 8

Data Representation Type of Data Representation Integers Bits Unsigned 2 s Comp Excess 7 Excess 8 Data Representation At its most basic level, all digital information must reduce to 0s and 1s, which can be discussed as binary, octal, or hex data. There s no practical limit on how it can be interpreted

More information

Under the Hood: Data Representation. Computer Science 104 Lecture 2

Under the Hood: Data Representation. Computer Science 104 Lecture 2 Under the Hood: Data Representation Computer Science 104 Lecture 2 Admin Piazza, Sakai Up Everyone should have access Homework 1 Posted Due Feb 6 PDF or Plain Text Only: No Word or RTF Recommended: Learn

More information

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring: ECE 120 Midterm 1 HKN Review Session Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location: Your Room on Compass What to bring: icard, pens/pencils, Cheat sheet (Handwritten) Overview of Review Binary IEEE

More information

printf( Please enter another number: ); scanf( %d, &num2);

printf( Please enter another number: ); scanf( %d, &num2); CIT 593 Intro to Computer Systems Lecture #13 (11/1/12) Now that we've looked at how an assembly language program runs on a computer, we're ready to move up a level and start working with more powerful

More information

More about Binary 9/6/2016

More about Binary 9/6/2016 More about Binary 9/6/2016 Unsigned vs. Two s Complement 8-bit example: 1 1 0 0 0 0 1 1 2 7 +2 6 + 2 1 +2 0 = 128+64+2+1 = 195-2 7 +2 6 + 2 1 +2 0 = -128+64+2+1 = -61 Why does two s complement work this

More information

Binary. Hexadecimal BINARY CODED DECIMAL

Binary. Hexadecimal BINARY CODED DECIMAL Logical operators Common arithmetic operators, like plus, minus, multiply and divide, works in any number base but the binary number system provides some further operators, called logical operators. Meaning

More information

Mathematics. Name: Class: Transforming Life chances

Mathematics. Name: Class: Transforming Life chances Mathematics Name: Class: Transforming Life chances Children first- Aspire- Challenge- Achieve Aspire: To be the best I can be in everything that I try to do. To use the adults and resources available both

More information

Number Systems. Both numbers are positive

Number Systems. Both numbers are positive Number Systems Range of Numbers and Overflow When arithmetic operation such as Addition, Subtraction, Multiplication and Division are performed on numbers the results generated may exceed the range of

More information

Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers

Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers Chapter 03: Computer Arithmetic Lesson 09: Arithmetic using floating point numbers Objective To understand arithmetic operations in case of floating point numbers 2 Multiplication of Floating Point Numbers

More information

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used. Declaration Fundamental Data Types All variables must be declared before being used. Tells compiler to set aside an appropriate amount of space in memory to hold a value. Enables the compiler to perform

More information

Numerical computing. How computers store real numbers and the problems that result

Numerical computing. How computers store real numbers and the problems that result Numerical computing How computers store real numbers and the problems that result The scientific method Theory: Mathematical equations provide a description or model Experiment Inference from data Test

More information

Memory Addressing, Binary, and Hexadecimal Review

Memory Addressing, Binary, and Hexadecimal Review C++ By A EXAMPLE Memory Addressing, Binary, and Hexadecimal Review You do not have to understand the concepts in this appendix to become well-versed in C++. You can master C++, however, only if you spend

More information

Inf2C - Computer Systems Lecture 2 Data Representation

Inf2C - Computer Systems Lecture 2 Data Representation Inf2C - Computer Systems Lecture 2 Data Representation Boris Grot School of Informatics University of Edinburgh Last lecture Moore s law Types of computer systems Computer components Computer system stack

More information

Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2

Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2 Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2 Instructor: Rob Bergen Applied Computer Science University of Winnipeg Announcements Website is up

More information

Chapter 3. Fundamental Data Types

Chapter 3. Fundamental Data Types Chapter 3. Fundamental Data Types Byoung-Tak Zhang TA: Hanock Kwak Biointelligence Laboratory School of Computer Science and Engineering Seoul National Univertisy http://bi.snu.ac.kr Variable Declaration

More information

Floating-Point Data Representation and Manipulation 198:231 Introduction to Computer Organization Lecture 3

Floating-Point Data Representation and Manipulation 198:231 Introduction to Computer Organization Lecture 3 Floating-Point Data Representation and Manipulation 198:231 Introduction to Computer Organization Instructor: Nicole Hynes nicole.hynes@rutgers.edu 1 Fixed Point Numbers Fixed point number: integer part

More information

l l l l l l l Base 2; each digit is 0 or 1 l Each bit in place i has value 2 i l Binary representation is used in computers

l l l l l l l Base 2; each digit is 0 or 1 l Each bit in place i has value 2 i l Binary representation is used in computers 198:211 Computer Architecture Topics: Lecture 8 (W5) Fall 2012 Data representation 2.1 and 2.2 of the book Floating point 2.4 of the book Computer Architecture What do computers do? Manipulate stored information

More information

Exponential Numbers ID1050 Quantitative & Qualitative Reasoning

Exponential Numbers ID1050 Quantitative & Qualitative Reasoning Exponential Numbers ID1050 Quantitative & Qualitative Reasoning In what ways can you have $2000? Just like fractions, you can have a number in some denomination Number Denomination Mantissa Power of 10

More information

Digital Fundamentals

Digital Fundamentals Digital Fundamentals Tenth Edition Floyd Chapter 2 2009 Pearson Education, Upper 2008 Pearson Saddle River, Education NJ 07458. All Rights Reserved Decimal Numbers The position of each digit in a weighted

More information

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: Bits and Bytes and Numbers

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: Bits and Bytes and Numbers Computer Science 324 Computer Architecture Mount Holyoke College Fall 2007 Topic Notes: Bits and Bytes and Numbers Number Systems Much of this is review, given the 221 prerequisite Question: how high can

More information

Floating Point Numbers

Floating Point Numbers Floating Point Numbers Summer 8 Fractional numbers Fractional numbers fixed point Floating point numbers the IEEE 7 floating point standard Floating point operations Rounding modes CMPE Summer 8 Slides

More information

LAB A Translating Data to Binary

LAB A Translating Data to Binary LAB A Translating Data to Binary Create a directory for this lab and perform in it the following groups of tasks: LabA1.java 1. Write the Java app LabA1 that takes an int via a command-line argument args[0]

More information

Topic Notes: Bits and Bytes and Numbers

Topic Notes: Bits and Bytes and Numbers Computer Science 220 Assembly Language & Comp Architecture Siena College Fall 2011 Topic Notes: Bits and Bytes and Numbers Binary Basics At least some of this will be review for most of you, but we start

More information

Physics 306 Computing Lab 1: Hello, World!

Physics 306 Computing Lab 1: Hello, World! 1. Introduction Physics 306 Computing Lab 1: Hello, World! In today s lab, you will learn how to write simple programs, to compile them, and to run them. You will learn about input and output, variables,

More information

Hexadecimal Numbers. Journal: If you were to extend our numbering system to more digits, what digits would you use? Why those?

Hexadecimal Numbers. Journal: If you were to extend our numbering system to more digits, what digits would you use? Why those? 9/10/18 1 Binary and Journal: If you were to extend our numbering system to more digits, what digits would you use? Why those? Hexadecimal Numbers Check Homework 3 Binary Numbers A binary (base-two) number

More information

Chapter 7. Basic Types

Chapter 7. Basic Types Chapter 7 Basic Types Dr. D. J. Jackson Lecture 7-1 Basic Types C s basic (built-in) types: Integer types, including long integers, short integers, and unsigned integers Floating types (float, double,

More information

Topic Notes: Bits and Bytes and Numbers

Topic Notes: Bits and Bytes and Numbers Computer Science 220 Assembly Language & Comp Architecture Siena College Fall 2010 Topic Notes: Bits and Bytes and Numbers Binary Basics At least some of this will be review, but we will go over it for

More information

Time (self-scheduled): Location Schedule Your Exam: What to bring:

Time (self-scheduled): Location Schedule Your Exam: What to bring: ECE 120 Midterm 1B HKN Review Session Time (self-scheduled): Between Wednesday, September 27 and Friday, September 29, 2017 Location: 57 Grainger Engineering Library (in the basement on the east side)

More information

Chapter 2. Data Representation in Computer Systems

Chapter 2. Data Representation in Computer Systems Chapter 2 Data Representation in Computer Systems Chapter 2 Objectives Understand the fundamentals of numerical data representation and manipulation in digital computers. Master the skill of converting

More information

Signed umbers. Sign/Magnitude otation

Signed umbers. Sign/Magnitude otation Signed umbers So far we have discussed unsigned number representations. In particular, we have looked at the binary number system and shorthand methods in representing binary codes. With m binary digits,

More information

The type of all data used in a C (or C++) program must be specified

The type of all data used in a C (or C++) program must be specified The type of all data used in a C (or C++) program must be specified A data type is a description of the data being represented That is, a set of possible values and a set of operations on those values

More information

Integers. N = sum (b i * 2 i ) where b i = 0 or 1. This is called unsigned binary representation. i = 31. i = 0

Integers. N = sum (b i * 2 i ) where b i = 0 or 1. This is called unsigned binary representation. i = 31. i = 0 Integers So far, we've seen how to convert numbers between bases. How do we represent particular kinds of data in a certain (32-bit) architecture? We will consider integers floating point characters What

More information

CS 31: Intro to Systems Binary Arithmetic. Martin Gagné Swarthmore College January 24, 2016

CS 31: Intro to Systems Binary Arithmetic. Martin Gagné Swarthmore College January 24, 2016 CS 31: Intro to Systems Binary Arithmetic Martin Gagné Swarthmore College January 24, 2016 Unsigned Integers Suppose we had one byte Can represent 2 8 (256) values If unsigned (strictly non-negative):

More information

15213 Recitation 2: Floating Point

15213 Recitation 2: Floating Point 15213 Recitation 2: Floating Point 1 Introduction This handout will introduce and test your knowledge of the floating point representation of real numbers, as defined by the IEEE standard. This information

More information

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types

More information

COMP2611: Computer Organization. Data Representation

COMP2611: Computer Organization. Data Representation COMP2611: Computer Organization Comp2611 Fall 2015 2 1. Binary numbers and 2 s Complement Numbers 3 Bits: are the basis for binary number representation in digital computers What you will learn here: How

More information

The type of all data used in a C++ program must be specified

The type of all data used in a C++ program must be specified The type of all data used in a C++ program must be specified A data type is a description of the data being represented That is, a set of possible values and a set of operations on those values There are

More information

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Floating Point. CSE 351 Autumn Instructor: Justin Hsia Floating Point CSE 351 Autumn 2017 Instructor: Justin Hsia Teaching Assistants: Lucas Wotton Michael Zhang Parker DeWilde Ryan Wong Sam Gehman Sam Wolfson Savanna Yee Vinny Palaniappan http://xkcd.com/571/

More information

Where are we? Compiler. translating source code (C or Java) Programs to assembly language And linking your code to Library code

Where are we? Compiler. translating source code (C or Java) Programs to assembly language And linking your code to Library code Where are we? Compiler Instruction set architecture (e.g., MIPS) translating source code (C or Java) Programs to assembly language And linking your code to Library code How the software talks To the hardware

More information

Variables and Data Representation

Variables and Data Representation You will recall that a computer program is a set of instructions that tell a computer how to transform a given set of input into a specific output. Any program, procedural, event driven or object oriented

More information

Slide Set 11. for ENCM 369 Winter 2015 Lecture Section 01. Steve Norman, PhD, PEng

Slide Set 11. for ENCM 369 Winter 2015 Lecture Section 01. Steve Norman, PhD, PEng Slide Set 11 for ENCM 369 Winter 2015 Lecture Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Winter Term, 2015 ENCM 369 W15 Section

More information

Beginning C Programming for Engineers

Beginning C Programming for Engineers Beginning Programming for Engineers R. Lindsay Todd Lecture 6: Bit Operations R. Lindsay Todd () Beginning Programming for Engineers Beg 6 1 / 32 Outline Outline 1 Place Value Octal Hexadecimal Binary

More information

Introduction to Scientific Computing Lecture 1

Introduction to Scientific Computing Lecture 1 Introduction to Scientific Computing Lecture 1 Professor Hanno Rein Last updated: September 10, 2017 1 Number Representations In this lecture, we will cover two concept that are important to understand

More information

Chapter 4. Operations on Data

Chapter 4. Operations on Data Chapter 4 Operations on Data 1 OBJECTIVES After reading this chapter, the reader should be able to: List the three categories of operations performed on data. Perform unary and binary logic operations

More information

Chapter 1. Numeric Artifacts. 1.1 Introduction

Chapter 1. Numeric Artifacts. 1.1 Introduction Chapter 1 Numeric Artifacts 1.1 Introduction Virtually all solutions to problems in electromagnetics require the use of a computer. Even when an analytic or closed form solution is available which is nominally

More information

Floating Point Numbers

Floating Point Numbers Floating Point Numbers Computer Systems Organization (Spring 2016) CSCI-UA 201, Section 2 Instructor: Joanna Klukowska Slides adapted from Randal E. Bryant and David R. O Hallaron (CMU) Mohamed Zahran

More information

Floating Point Numbers

Floating Point Numbers Floating Point Numbers Computer Systems Organization (Spring 2016) CSCI-UA 201, Section 2 Fractions in Binary Instructor: Joanna Klukowska Slides adapted from Randal E. Bryant and David R. O Hallaron (CMU)

More information

Machine Arithmetic 8/31/2007

Machine Arithmetic 8/31/2007 Machine Arithmetic 8/31/2007 1 Opening Discussion Let's look at some interclass problems. If you played with your program some you probably found that it behaves oddly in some regards. Why is this? What

More information

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning 4 Operations On Data 4.1 Foundations of Computer Science Cengage Learning Objectives After studying this chapter, the student should be able to: List the three categories of operations performed on data.

More information

Chapter 3: Arithmetic for Computers

Chapter 3: Arithmetic for Computers Chapter 3: Arithmetic for Computers Objectives Signed and Unsigned Numbers Addition and Subtraction Multiplication and Division Floating Point Computer Architecture CS 35101-002 2 The Binary Numbering

More information

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning 4 Operations On Data 4.1 Foundations of Computer Science Cengage Learning Objectives After studying this chapter, the student should be able to: List the three categories of operations performed on data.

More information

Basic data types. Building blocks of computation

Basic data types. Building blocks of computation Basic data types Building blocks of computation Goals By the end of this lesson you will be able to: Understand the commonly used basic data types of C++ including Characters Integers Floating-point values

More information

2. MACHINE REPRESENTATION OF TYPICAL ARITHMETIC DATA FORMATS (NATURAL AND INTEGER NUMBERS).

2. MACHINE REPRESENTATION OF TYPICAL ARITHMETIC DATA FORMATS (NATURAL AND INTEGER NUMBERS). 2. MACHINE REPRESENTATION OF TYPICAL ARITHMETIC DATA FORMATS (NATURAL AND INTEGER NUMBERS). 2.. Natural Binary Code (NBC). The positional code with base 2 (B=2), introduced in Exercise, is used to encode

More information

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Floating Point. CSE 351 Autumn Instructor: Justin Hsia Floating Point CSE 351 Autumn 2017 Instructor: Justin Hsia Teaching Assistants: Lucas Wotton Michael Zhang Parker DeWilde Ryan Wong Sam Gehman Sam Wolfson Savanna Yee Vinny Palaniappan Administrivia Lab

More information

8/30/2016. In Binary, We Have A Binary Point. ECE 120: Introduction to Computing. Fixed-Point Representations Support Fractions

8/30/2016. In Binary, We Have A Binary Point. ECE 120: Introduction to Computing. Fixed-Point Representations Support Fractions University of Illinois at Urbana-Champaign Dept. of Electrical and Computer Engineering ECE 120: Introduction to Computing Fixed- and Floating-Point Representations In Binary, We Have A Binary Point Let

More information

Variables and literals

Variables and literals Demo lecture slides Although I will not usually give slides for demo lectures, the first two demo lectures involve practice with things which you should really know from G51PRG Since I covered much of

More information

ECE232: Hardware Organization and Design

ECE232: Hardware Organization and Design ECE232: Hardware Organization and Design Lecture 11: Floating Point & Floating Point Addition Adapted from Computer Organization and Design, Patterson & Hennessy, UCB Last time: Single Precision Format

More information

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003 Objects and Types COMS W1007 Introduction to Computer Science Christopher Conway 29 May 2003 Java Programs A Java program contains at least one class definition. public class Hello { public static void

More information

A Java program contains at least one class definition.

A Java program contains at least one class definition. Java Programs Identifiers Objects and Types COMS W1007 Introduction to Computer Science Christopher Conway 29 May 2003 A Java program contains at least one class definition. public class Hello { public

More information

Computer Organization and Levels of Abstraction

Computer Organization and Levels of Abstraction Computer Organization and Levels of Abstraction Announcements Today: PS 7 Lab 8: Sound Lab tonight bring machines and headphones! PA 7 Tomorrow: Lab 9 Friday: PS8 Today (Short) Floating point review Boolean

More information

LING 388: Computers and Language. Lecture 5

LING 388: Computers and Language. Lecture 5 LING 388: Computers and Language Lecture 5 Administrivia Homework 3 graded Quick Homework 4 out today I'll be away next two weeks (my apologies) Colton Flowers, a HLT student, will take you through Python

More information

UNIT 7A Data Representation: Numbers and Text. Digital Data

UNIT 7A Data Representation: Numbers and Text. Digital Data UNIT 7A Data Representation: Numbers and Text 1 Digital Data 10010101011110101010110101001110 What does this binary sequence represent? It could be: an integer a floating point number text encoded with

More information

Divisibility Rules and Their Explanations

Divisibility Rules and Their Explanations Divisibility Rules and Their Explanations Increase Your Number Sense These divisibility rules apply to determining the divisibility of a positive integer (1, 2, 3, ) by another positive integer or 0 (although

More information

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture) COSC 243 Data Representation 3 Lecture 3 - Data Representation 3 1 Data Representation Test Material Lectures 1, 2, and 3 Tutorials 1b, 2a, and 2b During Tutorial a Next Week 12 th and 13 th March If you

More information

The Building Blocks: Binary Numbers, Boolean Logic, and Gates. Purpose of Chapter. External Representation of Information.

The Building Blocks: Binary Numbers, Boolean Logic, and Gates. Purpose of Chapter. External Representation of Information. The Building Blocks: Binary Numbers, Boolean Logic, and Gates Chapter 4 Representing Information The Binary Numbering System Boolean Logic and Gates Building Computer Circuits Control Circuits CMPUT Introduction

More information

Graphics calculator instructions

Graphics calculator instructions Graphics calculator instructions Contents: A B C D E F G Basic calculations Basic functions Secondary function and alpha keys Memory Lists Statistical graphs Working with functions 10 GRAPHICS CALCULATOR

More information

Applied Computer Programming

Applied Computer Programming Applied Computer Programming Representation of Numbers. Bitwise Operators Course 07 Lect.eng. Adriana ALBU, PhD Politehnica University Timisoara Internal representation All data, of any type, processed

More information

These are reserved words of the C language. For example int, float, if, else, for, while etc.

These are reserved words of the C language. For example int, float, if, else, for, while etc. Tokens in C Keywords These are reserved words of the C language. For example int, float, if, else, for, while etc. Identifiers An Identifier is a sequence of letters and digits, but must start with a letter.

More information

Chapter 2: Number Systems

Chapter 2: Number Systems Chapter 2: Number Systems Logic circuits are used to generate and transmit 1s and 0s to compute and convey information. This two-valued number system is called binary. As presented earlier, there are many

More information

Introduction to Computers and Programming. Numeric Values

Introduction to Computers and Programming. Numeric Values Introduction to Computers and Programming Prof. I. K. Lundqvist Lecture 5 Reading: B pp. 47-71 Sept 1 003 Numeric Values Storing the value of 5 10 using ASCII: 00110010 00110101 Binary notation: 00000000

More information

Chapter 3 Basic Data Types. Lecture 3 1

Chapter 3 Basic Data Types. Lecture 3 1 Chapter 3 Basic Data Types Lecture 3 1 Topics Scalar Types in C Integers Bit Operations Floating Point Types Conversions Lecture 4 2 Scalar Types in C The amount of memory available for a variable depends

More information

Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

Excerpt from Art of Problem Solving Volume 1: the Basics 2014 AoPS Inc. Chapter 5 Using the Integers In spite of their being a rather restricted class of numbers, the integers have a lot of interesting properties and uses. Math which involves the properties of integers is

More information

The. Binary. Number System

The. Binary. Number System The Binary Number System Why is Binary important? Everything on a computer (or other digital device) is represented by Binary Numbers One to Five in various systems 1 2 3 4 5 I II III IV V 1 10 11 100

More information

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng Slide Set 2 for ENCM 335 in Fall 2018 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2018 ENCM 335 Fall 2018 Slide Set 2 slide

More information

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C Chapter 11 Introduction to Programming in C C: A High-Level Language Gives symbolic names for containers of values don t need to know which register or memory location Provides abstraction of underlying

More information

COMP2121: Microprocessors and Interfacing. Number Systems

COMP2121: Microprocessors and Interfacing. Number Systems COMP2121: Microprocessors and Interfacing Number Systems http://www.cse.unsw.edu.au/~cs2121 Lecturer: Hui Wu Session 2, 2017 1 1 Overview Positional notation Decimal, hexadecimal, octal and binary Converting

More information

CHAPTER 2 Data Representation in Computer Systems

CHAPTER 2 Data Representation in Computer Systems CHAPTER 2 Data Representation in Computer Systems 2.1 Introduction 37 2.2 Positional Numbering Systems 38 2.3 Decimal to Binary Conversions 38 2.3.1 Converting Unsigned Whole Numbers 39 2.3.2 Converting

More information

MACHINE LEVEL REPRESENTATION OF DATA

MACHINE LEVEL REPRESENTATION OF DATA MACHINE LEVEL REPRESENTATION OF DATA CHAPTER 2 1 Objectives Understand how integers and fractional numbers are represented in binary Explore the relationship between decimal number system and number systems

More information

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands Introduction Operators are the symbols which operates on value or a variable. It tells the compiler to perform certain mathematical or logical manipulations. Can be of following categories: Unary requires

More information

CS 261 Fall Floating-Point Numbers. Mike Lam, Professor.

CS 261 Fall Floating-Point Numbers. Mike Lam, Professor. CS 261 Fall 2018 Mike Lam, Professor https://xkcd.com/217/ Floating-Point Numbers Floating-point Topics Binary fractions Floating-point representation Conversions and rounding error Binary fractions Now

More information

CHAPTER 2 Data Representation in Computer Systems

CHAPTER 2 Data Representation in Computer Systems CHAPTER 2 Data Representation in Computer Systems 2.1 Introduction 37 2.2 Positional Numbering Systems 38 2.3 Decimal to Binary Conversions 38 2.3.1 Converting Unsigned Whole Numbers 39 2.3.2 Converting

More information

Intermediate Algebra. Gregg Waterman Oregon Institute of Technology

Intermediate Algebra. Gregg Waterman Oregon Institute of Technology Intermediate Algebra Gregg Waterman Oregon Institute of Technology c 2017 Gregg Waterman This work is licensed under the Creative Commons Attribution 4.0 International license. The essence of the license

More information

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators Operators Overview Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators Operands and Operators Mathematical or logical relationships

More information

Course Schedule. CS 221 Computer Architecture. Week 3: Plan. I. Hexadecimals and Character Representations. Hexadecimal Representation

Course Schedule. CS 221 Computer Architecture. Week 3: Plan. I. Hexadecimals and Character Representations. Hexadecimal Representation Course Schedule CS 221 Computer Architecture Week 3: Information Representation (2) Fall 2001 W1 Sep 11- Sep 14 Introduction W2 Sep 18- Sep 21 Information Representation (1) (Chapter 3) W3 Sep 25- Sep

More information

Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators

Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators Formatted Input and Output The printf function The scanf function Arithmetic and Assignment Operators Simple Assignment Side Effect

More information

Reals 1. Floating-point numbers and their properties. Pitfalls of numeric computation. Horner's method. Bisection. Newton's method.

Reals 1. Floating-point numbers and their properties. Pitfalls of numeric computation. Horner's method. Bisection. Newton's method. Reals 1 13 Reals Floating-point numbers and their properties. Pitfalls of numeric computation. Horner's method. Bisection. Newton's method. 13.1 Floating-point numbers Real numbers, those declared to be

More information

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University Fundamental Data Types CSE 130: Introduction to Programming in C Stony Brook University Program Organization in C The C System C consists of several parts: The C language The preprocessor The compiler

More information

At the end of this module, the student should be able to:

At the end of this module, the student should be able to: INTRODUCTION One feature of the C language which can t be found in some other languages is the ability to manipulate pointers. Simply stated, pointers are variables that store memory addresses. This is

More information

Module 6: Array in C

Module 6: Array in C 1 Table of Content 1. Introduction 2. Basics of array 3. Types of Array 4. Declaring Arrays 5. Initializing an array 6. Processing an array 7. Summary Learning objectives 1. To understand the concept of

More information

Computer System and programming in C

Computer System and programming in C 1 Basic Data Types Integral Types Integers are stored in various sizes. They can be signed or unsigned. Example Suppose an integer is represented by a byte (8 bits). Leftmost bit is sign bit. If the sign

More information

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS Aleksandar Milenković The LaCASA Laboratory, ECE Department, The University of Alabama in Huntsville Email: milenka@uah.edu Web:

More information

Final Labs and Tutors

Final Labs and Tutors ICT106 Fundamentals of Computer Systems - Topic 2 REPRESENTATION AND STORAGE OF INFORMATION Reading: Linux Assembly Programming Language, Ch 2.4-2.9 and 3.6-3.8 Final Labs and Tutors Venue and time South

More information

Midterm Exam Answers Instructor: Randy Shepherd CSCI-UA.0201 Spring 2017

Midterm Exam Answers Instructor: Randy Shepherd CSCI-UA.0201 Spring 2017 Section 1: Multiple choice (select any that apply) - 20 points 01. Representing 10 using the 4 byte unsigned integer encoding and using 4 byte two s complements encoding yields the same bit pattern. (a)

More information