PHY281Variables operators and math functions Slide 1 More On Variables and Operators, And Maths Functions In this section we will learn more about variables.

Slides:



Advertisements
Similar presentations
Craig Schock, 2003 Binary Numbers Numbering Systems Counting Symbolic Bases Common Bases (10, 2, 8, 16) Representing Information Binary to Decimal Conversions.
Advertisements

Connecting with Computer Science, 2e
Level ISA3: Information Representation
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
Binary Numbers 1.Binary and Denary RecapBinary.
Connecting with Computer Science 2 Objectives Learn why numbering systems are important to understand Refresh your knowledge of powers of numbers Learn.
(2.1) Fundamentals  Terms for magnitudes – logarithms and logarithmic graphs  Digital representations – Binary numbers – Text – Analog information 
Simple Data Type Representation and conversion of numbers
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
The character data type char
Computers Organization & Assembly Language
Computer Arithmetic Nizamettin AYDIN
Digital Design: From Gates to Intelligent Machines
1 Digital Technology and Computer Fundamentals Chapter 1 Data Representation and Numbering Systems.
IT253: Computer Organization
Dept. of Computer Science Engineering Islamic Azad University of Mashhad 1 DATA REPRESENTATION Dept. of Computer Science Engineering Islamic Azad University.
EX_01.1/46 Numeric Systems. EX_01.2/46 Overview Numeric systems – general, Binary numbers, Octal numbers, Hexadecimal system, Data units, ASCII code,
Lec 3: Data Representation Computer Organization & Assembly Language Programming.
Number Systems Spring Semester 2013Programming and Data Structure1.
CS151 Introduction to Digital Design
CSC 221 Computer Organization and Assembly Language
1 Data Representation Characters, Integers and Real Numbers Binary Number System Octal Number System Hexadecimal Number System Powered by DeSiaMore.
1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)
Data Representation, Number Systems and Base Conversions
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Number Systems Denary Base 10 Binary Base 2 Hexadecimal Base 16
1 Information Representation in Computer Lecture Nine.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
CS 2130 Lecture 23 Data Types.
Systems Architecture, Fourth Edition 1 Data Representation Chapter 3.
Chapter 1 Representing Data in a Computer. 1.1 Binary and Hexadecimal Numbers.
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.
PRIMITIVE TYPES IN JAVA Primitive Types Operations on Primitive Types.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Floating Point Representations
Data Representation COE 308 Computer Architecture
Variables, Operators, and Expressions
Bits, Data Types, and Operations
Chapter 2 Bits, Data Types, and Operations
Machine level representation of data Character representation
Programming and Data Structure
Chapter 3 Data Representation Text Characters
NUMBER SYSTEMS.
Data Representation ICS 233
Lec 3: Data Representation
Data Representation.
Binary Numbers The arithmetic used by computers differs in some ways from that used by people. Computers perform operations on numbers with finite and.
3.1 Denary, Binary and Hexadecimal Number Systems
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Chapter 2 Data Types and Representations
Multiple variables can be created in one declaration
Data Representation COE 301 Computer Organization
Introduction to Java, and DrJava part 1
Cosc 2P12 Week 2.
Number Systems Lecture 2.
Chapter 3 DataStorage Foundations of Computer Science ã Cengage Learning.
Introduction to Java, and DrJava
Data Representation ICS 233
Introduction to Computer Engineering
Rayat Shikshan Sanstha’s S. M. Joshi College, Hadapsar
Introduction to Java, and DrJava part 1
Text Representation ASCII Collating Sequence
Cosc 2P12 Week 2.
Chapter 3 - Binary Numbering System
Data Representation COE 308 Computer Architecture
Chapter 2 Bits, Data Types, and Operations
Presentation transcript:

PHY281Variables operators and math functions Slide 1 More On Variables and Operators, And Maths Functions In this section we will learn more about variables in memory, more on the operators in Java and something about the maths functions available:  Binary and hexadecimal numbers  Floating Point Numbers  Text  The division operator  Type conversion operators  Prefix and postfix operators  Assignment operators  Maths functions

PHY281Variables operators and math functions Slide 2 Binary Numbers Memory consists of thousands of switches (transistors) which are either on (=1) or off (=0) - called binary digits or bits. Eight such bits = One byte. 1 byte can represent 0 to 255 decimal (256 values in total). Larger numbers are stored in words which consist of several bytes (often 4). Hence Windows which uses 4 bytes per word is known as a 32 bit (8 X 4) operating system. Hence 3 is = is = Byte: Bit: Value: Decimal: Most significant bitLeast significant bit

PHY281Variables operators and math functions Slide 3 Hexadecimal Numbers With larger binary numbers it is better to use Hexadecimal (base 16) notation. Each digit can have values from 0 to 15 (0 to 9 then A, B, C, D, E, F). Each four binary digits is represented by one hexadecimal digit. Digit: Value: Decimal: e.g. 16,103,905 decimal is Binary F 5 B 9 E 1 i.e. F5B9E1 in Hexadecimal Check : 15 X X X X X = 16,103,905 Dec Hex A 11 B 12 C 13 D 14 E 15 F

PHY281Variables operators and math functions Slide 4 Binary and Hexadecimal Decimal Binary Hexadecimal A B C D E F Decimal Binary Hexadecimal FF

PHY281Variables operators and math functions Slide 5 Binary Arithmetic At their lowest level computers cannot Subtract, Multiply or Divide - only Add (but very fast). Addition is done bit wise in bytes or words. Addition = Computers cannot subtract. However, they can negate a number and then add it to achieve a subtraction e.g becomes 6 + (-5)

PHY281Variables operators and math functions Slide 6 Binary Subtraction In order to allow negative numbers, the leftmost (most significant) bit is designated the sign bit. Cannot simply set the sign bit to make a number negative. 6: : = -11 Wrong Use 2’s compliment - which is 1’s compliment (change all 0 to 1 and vice versa) plus 1. 5: ’s complement: Add Hence -5 is: : : = 1 5: : = 0 Subtract = 0 Subtract = 1 Carry over to left is thrown away Instead of representing 0 to 255 decimal 1 byte now represents -128 to +127 (Still 256 values in total).

PHY281Variables operators and math functions Slide 7 Binary Multiplication Since computers can only add, the simplest way to multiply is to add repeatedly: i.e. 4 X 6 = = 24 6: : : : Could be very time consuming for large numbers. A better algorithm is to use partial fractions. In decimal 23 X 125 is 23 X 1 X 100 plus 23 X 2 X 10 plus 23 X 5 X 1 = In binary one can use this technique by taking one number and bit shifting it according to the position of each bit in the other number and then summing them all up. 23 X 125 is X = 2875

PHY281Variables operators and math functions Slide 8 Binary Division Division Simplest method to divide 42 by 7: keep subtracting 7 from (adding -7 to) 42 till it reaches 0 and count how many times you did it. In reality modern computers have dedicated hardware to perform arithmetic calculations such as multiplication and division.

PHY281Variables operators and math functions Slide 9 Floating Point Numbers The numbers we have represented in binary so far such as 0, 6, 125, -5 are whole numbers known as Integers. How do computers represent Floating Point numbers such as 12.2, 3.142, 0.5, , x ? First they are converted to a standard form: x 10 2, 0.31 x 10 1, 0.5 x 10 0, -0.1 x 10 -2, x which are usually written 0.122E02, 0.31E01, 0.5E00, -0.1E-02, E-18 i.e. (Mantissa)E(Exponent). The actual representation varies with computer and programming language. In Java floating point numbers are stored as Sign x Mantissa x 2 Exponent with the different parts of the word storing the different components. E for Exponent - nothing to do with base e (natural logarithms)

PHY281Variables operators and math functions Slide 10 Floating Point Numbers For example, the representation of 32 bit floating point numbers in Java is: The 8 bits for the exponent part allow 256 different values (0 and 255 have special meanings.) The actual exponent (power of 2) is given by the EEEEEEEE part (bias) and hence range from -125 to 128 i.e (= 2.3 x ) to (= 3.4 x ). The Mantissa is calculated as: bit 23 x bit 22 x bit 22 x … + bit 2 x bit1 x The least significant bit (bit1) gives a value of = so these numbers are accurate to approximately 7 digits. In this system  would be represented as The exponent part is = 2. The mantissa is = = … = x 2 2 = SEEE EEEE EMMM MMMM MMMM MMMM MMMM Eight bits for the Exponent One Sign bit 23 bits for the Mantissa Bit 0 Bit 31

PHY281Variables operators and math functions Slide 11 Representing Text Computers can store integers and floating point numbers in binary but what about text e.g. your Word Document? Text is stored as individual characters A,a,B,b etc. Each character is stored in one byte (8 bits) according to the ASCII (American Standard Code for Information Exchange) Table. The normal characters, numbers and symbols, plus some control codes are stored in the first 128 characters (7 bits). Some languages such as Japanese cannot be accommodated in 8 bits so there is an extended version call Unicode which uses 2 bytes (16 bits or 65,535 characters). Each character, both uppercase and lowercase letters, even the space, has its own unique ASCII code. Note that the ASCII code for numerals is not the same as the integer representation of that number. The ASCII code for the character ‘9’ is (decimal 57) whereas the integer representation is If you press ‘A’ on your keyboard the ASCII value is sent to the computer and stored maybe in your Word Document or sent to the printer. The printer looks up which character the corresponds to before printing it.

PHY281Variables operators and math functions Slide 12 ASCII Codes Dec CharDec CharDec CharDec CharDec CharDec CharDec CharDec Char 000 NUL016 DLE P096 `112 p 001 SOH017 DC1033 ! A081 Q097 a113 q 002 STX018 DC2034 “ B082 R098 b114 r 003 ETX019 DC3035 # C083 S099 c115 s 004 EOT020 DC4036 $ D084 T100 d116 t 005 ENQ021 NAK037 % E085 U101 e117 u 006 ACK022 SYN038 & F086 V102 f118 v 007 BEL023 ETB039 ‘ G087 W103 g119 w 008 BS024 CAN040 ( H088 X104 h120 x 009 TAB025 EM041 ) I089 Y105 i121 y 010 LF026 SUB042 *058 :074 J090 Z106 j122 z 011 VT027 ESC ;075 K091 [107 k123 { 012 FF028 FS044,060 <076 L092 \108 l124 | 013 CR029 GS =077 M093 ]109 m125 } 014 SO030 RS >078 N094 ^110 n126 ~ 015 SI031 US047 /063 ?079 O o127 DEL The first 32 codes are control characters (mostly historical) - useful ones are TAB, LF (line feed or new line), FF (Form feed), CR (Carriage return).

PHY281Variables operators and math functions Slide 13 More on Operators You met your fist operators last week; we will now learn a bit more about them. Some of the things may seem a bit abstract for now, but they will be useful in the future.

PHY281Variables operators and math functions Slide 14 Integer Division If you divide two integers, the answer will be truncated to an integer value - this is usually NOT what you want. import java.awt.*; import java.applet.Applet; public class IntDiv extends Applet { public void paint(Graphics g) { int i = 2/3; double d1 = 2/3; double d2 = 2.0/3.0; g.drawString("i = " + i, 50, 50); g.drawString("d1 = " + d1, 50, 75); g.drawString("d2 = " + d2, 50, 100); } Try this:

PHY281Variables operators and math functions Slide 15 import java.awt.*; import java.applet.Applet; public class IntDiv extends Applet { public void paint(Graphics g) { int i = 2/3; double d1 = 2/3; double d2 = 2.0/3.0; g.drawString("i = " + i, 50, 50); g.drawString("d1 = " + d1, 50, 75); g.drawString("d2 = " + d2, 50, 100); } Integer Division Divides 2 by 3 then truncates it to store it as an integer. Since the RHS are both integers does an integer division as before. Then converts the result to a double.

PHY281Variables operators and math functions Slide 16 Type Conversion Sometimes you need to convert from one type to another. This is called casting and is done by putting the required type in brackets before the variable. double d1 = (double)2/3; int i = 2; double x; x = (double)i; This can be used to solve the integer division problem: Converts (casts) i to a double. Converts integer 2 to a double 2.0 before the division. The result is then a double. If you do the reverse and cast a double to an integer you truncate it and lose the decimal places. double x = 2.4; int i; i = (int)x; i becomes 2 and the 0.4 is lost.

PHY281Variables operators and math functions Slide 17 Type Conversion 2 A special case is converting a String into a number. Remember, a String is an object, not a variable, so you might have expected something different. String text =“21.22”; double x; x = Double.valueOf(text).doubleValue(); There are similar methods for converting the string to Floats, Boolians etc: Converts (casts) i to a double.

PHY281Variables operators and math functions Slide 18 Incrementing and Decrementing A common task is to increase a number by 1 (incrementing) or decrease it by 1 (decrementing). There are two operators for this: ++ Increment -- Decrement total = total + 1;total++; is equivalent to total = total - 1;total--; is equivalent to

PHY281Variables operators and math functions Slide 19 Prefix and Postfix int x = 3; int y = 3; int pre = ++x * 10; int post = y++ * 10; g.drawString("pre = " + pre + " x = " + x, 50, 50); g.drawString("post = " + post + " y = " + y, 50, 75); pre = 40 x = 4 post = 30 y = 4 Increments x before multiplication. Increments y after multiplication. To avoid confusion suggest you stick to postfix and don't use it in expressions. int post = y * 10; y++; If the ++ or -- comes after the variable (postfixed) the number is updated after any calculation. If they come before the variable (prefixed) the number is updated before the calculation.

PHY281Variables operators and math functions Slide 20 Operator Precedence int y = 10; int x = y * 3 + 5; 10 x 3 = 30 plus 5 = 35 or = 8 x 10 = 80? Answer 35. The following order (precedence) is used:  Incrementing and Decrementing first then  Multiplication, Division and Remainder then  Addition and Subtraction then  The equals sign to set a value. Operators with equal precedence are evaluated left to right int x = 4; int number = ++x * * 10 /2; 5 x 6 = = 50 4 x 5 = 20 If you want to change the precedence use brackets ( ). int y = 10; int x = y * (3 + 5); Now gives 80 What value is x?

PHY281Variables operators and math functions Slide 21 Assignment Operators In addition there are a set of 'assignment and operator' operators of the form = These are equivalent to = y = x; The simple assignment operator = sets the variable on the left of the = sign to the value of the variable or expression on the right of the = sign. x += 2; x -= 2; x *= 2; x /= 2; x %= 2; are equivalent to x = x + 2; x = x - 2; x = x * 2; x = x / 2; x = x % 2; myRatherLongName += 2;myRatherLongName = myRatherLongName + 2;

PHY281Variables operators and math functions Slide 22 Other Operators In addition there are: Comparison Operators - used to compare variables or objects < less than <= less than or equal to > greater than >= greater or equal to == equal to != not equal to Logical Operators - used to perform logical operations && AND || OR Bitwise Operators - used to perform binary operations. We shall use some of these when we make decisions later on.

PHY281Variables operators and math functions Slide 23 Mathematical Functions In scientific applications you will need to use certain mathematical functions like sine, cosine and log. In Java these are provided in the maths library. To use one of these functions you do not need an import statement as it is already included but you do need to precede the name with Math. like this: y = Math.sqrt(x); which calculates the square root of the parameter x. The most widely used functions are these (x is a floating point number): Math.cos(x)cosine of the angle x where x is in radians Math. sin(x)sine of the angle x where x is in radians Math. tan(x)tangent of the angle x where x is in radians Math. abs(x)the absolute value of x i.e. |x| in mathematics Math. min(x,y)the smaller of x and y. Math. max(x,y) the larger of x and y. Math. round(x)rounds a floating point number to the nearest integer. Math. log(x)natural (base e) logarithm of x. Math. random( )a pseudo random number in the range 0.0 to … Math. sqrt(x)the positive square root of x Math. pow(x,y)x raised to the power y i.e x y. Math. exp(x)e x. The library also provides the constants Math.E and Math.PI for the values of e and .