First project Create a JApplet with 3 textfields (2 for input and one for the answer), appropriate labels, and buttons for plus, minus and times. Your.

Slides:



Advertisements
Similar presentations
EET 1131 Unit 7 Arithmetic Operations and Circuits
Advertisements

The Binary Numbering Systems
DATA REPRESENTATION Y. Colette Lemard February
1 CSE1301 Computer Programming Lecture 29: Number Representation (Part 1)
Digital Fundamentals Floyd Chapter 2 Tenth Edition
Computer Structures Lecture 2: Working with 0’s and 1’s.
Data Representation Computer Organization &
Data Representation COE 205
Assembly Language and Computer Architecture Using C++ and Java

Signed Numbers.
Assembly Language and Computer Architecture Using C++ and Java
Computer ArchitectureFall 2008 © August 25, CS 447 – Computer Architecture Lecture 3 Computer Arithmetic (1)
1 Binary Arithmetic, Subtraction The rules for binary arithmetic are: = 0, carry = = 1, carry = = 1, carry = = 0, carry =
Computer ArchitectureFall 2007 © August 29, 2007 Karem Sakallah CS 447 – Computer Architecture.
1 Binary Numbers Again Recall that N binary digits (N bits) can represent unsigned integers from 0 to 2 N bits = 0 to 15 8 bits = 0 to bits.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.
Binary Arithmetic Math For Computers.
Topic 4 Computer Mathematics and Logic
Binary Representation and Computer Arithmetic
The Binary Number System
Data Representation Number Systems.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Integer Data representation Addition and Multiplication.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
© 2009 Pearson Education, Upper Saddle River, NJ All Rights ReservedFloyd, Digital Fundamentals, 10 th ed Digital Fundamentals Tenth Edition Floyd.
Simple Data Type Representation and conversion of numbers
Numbers and number systems
Lecture for Week Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday.
Data Representation – Binary Numbers
Computers Organization & Assembly Language
Computer Arithmetic Nizamettin AYDIN
#1 Lec # 2 Winter EECC341 - Shaaban Positional Number Systems A number system consists of an order set of symbols (digits) with relations.
IT253: Computer Organization
Lec 3: Data Representation Computer Organization & Assembly Language Programming.
Number Systems Spring Semester 2013Programming and Data Structure1.
CH09 Computer Arithmetic  CPU combines of ALU and Control Unit, this chapter discusses ALU The Arithmetic and Logic Unit (ALU) Number Systems Integer.
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.
Information Representation. Digital Hardware Systems Digital Systems Digital vs. Analog Waveforms Analog: values vary over a broad range continuously.
Computer Arithmetic and the Arithmetic Unit Lesson 2 - Ioan Despi.
BR 8/99 Binary Numbers Again Recall than N binary digits (N bits) can represent unsigned integers from 0 to 2 N bits = 0 to 15 8 bits = 0 to 255.
CSC 221 Computer Organization and Assembly Language
Floating Point Arithmetic
Computer Organization and Design Information Encoding II Montek Singh Wed, Aug 29, 2012 Lecture 3.
07/12/ Data Representation Two’s Complement & Binary Arithmetic.
1 2.1 Introduction A bit is the most basic unit of information in a computer. –It is a state of “on” or “off” in a digital circuit. –Sometimes these states.
WEEK #2 NUMBER SYSTEMS, OPERATION & CODES (PART 1)
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Digital Representations ME 4611 Binary Representation Only two states (0 and 1) Easy to implement electronically %0= (0) 10 %1= (1) 10 %10= (2) 10 %11=
Digital Fundamentals Tenth Edition Floyd Chapter 2 © 2008 Pearson Education.
CS1Q Computer Systems Lecture 2 Simon Gay. Lecture 2CS1Q Computer Systems - Simon Gay2 Binary Numbers We’ll look at some details of the representation.
IT1004: Data Representation and Organization Negative number representation.
Numbers in Computers.
09/03/20161 Information Representation Two’s Complement & Binary Arithmetic.
Number Systems. The position of each digit in a weighted number system is assigned a weight based on the base or radix of the system. The radix of decimal.
COSC2410: LAB 2 BINARY ARITHMETIC SIGNED NUMBERS FLOATING POINT REPRESENTATION BOOLEAN ALGEBRA 1.
Binary Arithmetic James A. Rome Tennessee Governor's Academy August 2010.
Binary Addition The simplest arithmetic operation in binary is addition. Adding two single-digit binary numbers is relatively simple, using a form of carrying:
Digital Logic & Design Dr. Waseem Ikram Lecture 02.
A Level Computing Component 2
CS1010 Programming Methodology
How to represent real numbers
How to represent real numbers
Digital Logic & Design Lecture 02.
Binary to Decimal Conversion
Two’s Complement & Binary Arithmetic
Presentation transcript:

First project Create a JApplet with 3 textfields (2 for input and one for the answer), appropriate labels, and buttons for plus, minus and times. Your JApplet will perform binary int computations: +, - and * For negative answers, do not show the two’s complement value, instead, negate it and show the answer in the appropriate textfield with a minus sign in front. See slides which follow regarding two’s complement representation.

Signed binary ints There are many possible signed int representations. We discuss 3 here, including the one your computer uses. Signed ints are positive or negative values. So the representation must include information on the sign of the number. In all 3 representations we discuss, the high order bit is reserved to indicated the sign: 0=positive, 1=negative. Notice, we have used one bit for the sign, so the magnitude of the value we can represent in a given number of bits is decreased by a power of 2. Remember the bit on the left is the msb (most significant bit) and the bit on the right is the lsb (least significant bit), whatever wordsize you are using is 8 bits, a byte. Which is the msb? Which bit is the lsb?

Sign-magnitude A simple way to represent signed ints might be to use the msb for the sign and the rest of the bits for the magnitude is 8 bits, with msb=1, so the number is negative. The value or magnitude is 10, so this is – and are respectively the smallest and largest possible values in 8 bits. What values (in decimal) are they? Why is arithmetic difficult to do in the sign-magnitude representation? Because of these problems this representation is not used in modern digital computers.

One’s complement This representation has some things in common with sign-magnitude: All the positive numbers look the same. Again, if the high order bit is a 0 then the number is positive, and the rest of the bits represent the value is a positive value in 8-bit one’s complement, and would be the same value in sign-magnitude. What value? is the negative representation for the same value. Negation is performed by flipping bits. Arithmetic works somewhat in one’s complement with some limitations and a curious problem. Can you find the problem?

Two’s Complement Your computer uses two’s complement to represent signed int values. Positive values look the same as in sign-magnitude and one’s complement. The process of negation (negating positive values to get negative, or negating negative values to get positive) is performed in two steps, correcting the funny arithmetic quirk of one’s complement arithmetic. Negate a value by flipping bits, then adding a represents what positive value? is the flip-bits, and is the negation of that value _________

Two’s Complement Arithmetic in two’s complement works, as long as you don’t add two positives or two negatives together which yield a sum too big for the wordsize. This would cause a carry into the sign bit, making a positive sum negative, for example. Practice adding some positive and negatives together. An extra value. In an 8 bit representation who is ? It looks like it should be negative, what happens when you flip the bits and add 1? In two’s complement, there is always one more negative value than positive, and in 8 bits, the range is –128…127 and in 16 bits …32767

Adding two binary values Adding numbers in any base works the same way. If x[] and y[] and sum[] are arrays of size SIZE filled with values base b then the following algorithm computes the sum of the values in x, and y. This algorithm assumes x and y are padded on the left with 0s. int tot,carry =0; for(int k=0;k<SIZE;k++){ tot=x[k]+y[k]+carry; sum[k]=tot%b;//b is the base carry=tot/b; }

More remarks on your first project You will get input as two Strings. You need to get the binary information out of these and put it into int arrays, remembering to pad the extra space in the array with zeros. An array is declared in Java like this: int []x=new int[20];//or whatever size you want You can declare it and allocate it later so you could make the global declaration: int x[]; And in another method, init() or actionPerformed() allocate the array: x=new int[20]; A complication. String subscripting starts on the left with 0 and goes up to StringName.length()-1. You will need to flip over or reverse the order of the bits in the Strings as you copy them into your arrays. Array subscripts go from 0 to arraylength-1

Methods for your project Write the following methods: Convert: takes a String and returns the int array the way you want it Sumup: adds two binary numbers (arrays) and returns the array sum. Do not call this method add, there already is an important Component method in Java named add. Flipbits: takes an int array parameter and returns the flipped bits version of the array Shift: Shifts the contents of a binary array by one and returns this new array (the old one*2)

About java methods (functions) Java does not support reference parameters. All parameters are value parameters. Java allows a function return type to be int, float, double, int[], or any classtype or datatype. Consider then the syntax for method operation in you first project. Suppose you have two int arrays, x and y. To make y be the flipbits of x, assuming you have a method called flipBits: y=flipBits(x); Somewhere in this method must appear the statement return with an array argument. Discuss how to do addition, subtraction and multiplication with the methods suggested on a previous slide.

Fractions A “fractional” value f, is one where 0<=f<1. If f<1 in one base, then obviously it is less than one in every base. The decimal fraction.5 can be written in binary as.1 Note that to the right of the “decimal” point, a digit d in position p in base b represents d*b p, where the first position to the right of the point is –1. In binary,.1 represents 1*2 -1 = 1/2 It is possible to convert fractions to/from base b to decimal.

Converting Fractions: Decimal to Another Base Consider converting the base 10 fraction.6842 to base two. Recall the binary fraction.1 =1/2 1/8 in binary is.001 and is equal to the decimal fraction = =.101 (2) + ? We could continue in this fashion guessing at the answer and whittling away but there is a much simpler way (10) =.b 1 b 2 b 3 b 4 … (2) Clearly we need values for each bit b in the answer. If we multiply both sides of the above equation by 2, on the right side the decimal point shifts right 1 position: 2*.6842 (10) = b 1. b 2 b 3 b 4 … (2). 2*.6842= so b 1 =1. Multiply.3684 by 2 to get so b 2 =0. Continue: 2*.7368= so b 3 =1. The next three iterations would give b 4 =0, b 5 =1 and b 6 =1 so our answer so far is.6842 (10) = (2)

Converting Fractions: Decimal to Another Base.6842 (10) = (2) What is the error in this approximation? How many binary bits should we give in an “accurate” answer? Can you devise a method to convert fractions in base 10 to any other base? Conversion decimal to binary can be done by repeatedly multiplying the decimal fraction by 2, peeling off the “bit” that appears each time to the left of the decimal point and making it the next bit of the answer, and continuing until you have enough bits.

Example.4298 = what in binary?.4298*2 = so the first bit of the answer is a *2 = so the second bit is a *2= so the third bit is a *2= so the next bit is also a 1. You can see that the next bit will be a 1 and the bit after a 0. So part of the binary fraction is …. How many bits should we try to get to achieve the same precision that 4 decimal digits has? Is there a formal mathematical way of saying it?

Converting Fractions: Decimal to Another Base Assuming a Java interface with TextFields named input (a base 10 fraction), baseval (the new base) and output (where the answer should appear) here is a java code fragment to do fraction conversions: String answer=”.”;//start answer with decimal point double fraction =Double.parseDouble(input.getText()); int base =Integer.parseInt(baseval.getText()); for (int j=0;j<????;j++)//still need to figure out how many digits {int temp= (int) fraction*base; int digit= temp<1?0:temp; fraction= fraction*base-digit; answer+=digit;} output.setText(answer);

A java method that manipulates arrays Although this example is not exactly useful for your program, it does show how to write a method that manipulates & returns an array argument. Suppose an int array values contains a bunch of decimal values and we are interested only in knowing whether each value is even or odd. I might want a boolean array of the same size called parity. My plan might be to put true in a position of parity if that position of values contains an even int and false if it is odd.

Code & comments //maybe in the global area appear the declarations int values[]; boolean parity[]; //maybe in init or somewhere else the arrays are allocated: values=new int[40]; parity =new boolean[40]; //we assume values has been filled, then somewhere or other is the function call parity=getParity(values); //somewhere appears the function definition: boolean [] getParity(int []x){ boolean temp[]=new boolean[x.length]; for(int k=0;k<x.length;k++) temp[k]=x[k]%2==0; return temp;}