18-May-15 Numbers. 2 Bits and bytes A bit is a single two-valued quantity: yes or no, true or false, on or off, high or low, good or bad One bit can distinguish.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Constants and Data Types Constants Data Types Reading for this class: L&L,
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Integer Types. Bits and bytes A bit is a single two-valued quantity: yes or no, true or false, on or off, high or low, good or bad One bit can distinguish.
Digital Fundamentals Floyd Chapter 2 Tenth Edition
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Connecting with Computer Science, 2e
Constants Variables change, constants don't final = ; final double PI = ; … area = radius * radius * PI; see Liang, p. 32 for full code.
Numbers and Arrays. Widening and narrowing Numeric types are arranged in a continuum: double float long int short byte, char You can easily assign a narrower.
Introduction to Programming with Java, for Beginners
22-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
28-Jun-15 Number Systems. 2 Bits and bytes A bit is a single two-valued quantity: yes or no, true or false, on or off, high or low, good or bad One bit.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Chapter 2 Data Types, Declarations, and Displays
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
String Escape Sequences
01- Intro-Java-part1 1 Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2008.
(2.1) Fundamentals  Terms for magnitudes – logarithms and logarithmic graphs  Digital representations – Binary numbers – Text – Analog information 
© 2009 Pearson Education, Upper Saddle River, NJ All Rights ReservedFloyd, Digital Fundamentals, 10 th ed Digital Fundamentals Tenth Edition Floyd.
***** SWTJC STEM ***** Chapter 2-3 cg 29 Java Operators Recall Java’s programming components: Packages - Collection of classes (Programs) Classes - Collections.
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
IT253: Computer Organization
VARIABLES AND TYPES CITS1001. Types in Java the eight primitive types the unlimited number of object types Values and References The Golden Rule Scope.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Copyright 2005 Curt Hill Binary, Octal and Hexadecimal The number bases used in computers.
Lecture 5. Topics Sec 1.4 Representing Information as Bit Patterns Representing Text Representing Text Representing Numeric Values Representing Numeric.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
How a Computer Processes Information. Java – Numbering Systems OBJECTIVE - Introduction to Numbering Systems and their relation to Computer Problems Review.
CS115 FALL Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3.
Binary01.ppt Decimal Decimal: Base 10 means 10 Unique numerical digits ,00010,000 Weight Positions 3,
Copyright © – Curt Hill Types What they do.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
1 Primitive Types n Four integer types:  byte  short  int (most common)  long n Two floating-point types:  float  double (most common) n One character.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
7. BASIC TYPES. Systems of numeration Numeric Types C’s basic types include integer types and floating types. Integer types can be either signed or unsigned.
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.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
1.  Algorithm: 1. Read in the radius 2. Compute the area using the following formula: area = radius x radius x PI 3. Display the area 2.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Primitive Types Four integer types: Two floating-point types:
Numbers Mar 27, 2013.
Chapter 2 Elementary Programming
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
EPSII 59:006 Spring 2004.
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Type Conversion, Constants, and the String Object
Fundamental Data Types
Numerical Data Types.
Introduction to Java, and DrJava part 1
Introduction to Java, and DrJava
Fundamental Data Types
Introduction to Primitives
Numbers 27-Apr-19.
Introduction to Java, and DrJava part 1
Numbers 6-May-19.
Presentation transcript:

18-May-15 Numbers

2 Bits and bytes A bit is a single two-valued quantity: yes or no, true or false, on or off, high or low, good or bad One bit can distinguish between two cases: T, F Two bits can distinguish between four cases: TT, TF, FT, FF Three bits can distinguish between eight cases: TTT, TTF, TFT, TFF, FTT, FTF, FFT, FFF In general, n bits can distinguish between 2 n cases A byte is 8 bits, therefore 2 8 = 256 cases

3 Number systems The binary (base 2) number system uses two “binary digits, ” (abbreviation: bits) -- 0 and 1 The octal (base 8) number system uses eight digits: 0, 1, 2, 3, 4, 5, 6, 7 The decimal (base 10) number system uses ten digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 The hexadecimal, or “hex” (base 16) number system uses sixteen digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F

4 Everything is a number? Everything in the computer is stored as a pattern of bits Binary distinctions are easy for hardware to work with Numbers are stored as a pattern of bits Computers use the binary number system Characters are stored as a pattern of bits One byte (8 bits) can represent one of 256 characters So, is everything in the computer stored as a number? No it isn’t, it’s stored as a bit pattern There are many ways to interpret a bit pattern

5 Counting To count up in any number system, add 1 to the rightmost digit if the result is higher than the largest digit, set that digit to zero and carry to the next place repeat addition of 1 and carrying as many times as necessary Example: In hex, 4A6FF + 1 = 4A700

6 Counting in three systems Dec Bin Hex A Dec Bin Hex B C D E F

7 Computers use binary numbers People like to use decimal numbers Computers use binary numbers Java translates decimal numbers into binary The computer does all its arithmetic in binary Java translates binary results back into decimal You occasionally have to use numbers in other number systems Colors are usually specified in hexadecimal notation: #FF0000, #669966,

8 Using octal and hex numbers Computers use binary, but the numbers are too long and confusing for people--it’s easy to lose your place Octal or hex is better for people Translation between binary and octal or hex is easy One octal digit equals three binary digits One hexadecimal digit equals four binary digits B 5 C A 0 B

9 Writing octal and hex integers Integers are usually written in decimal notation: 7, 532, -28 To write a number in octal, just start with a zero: 02, 0657, but don’t use the digits 8 or 9 ! To write a number in hexadecimal, start with 0x or 0X : 0xA, 0X43AB5, -0xFFFF The “digits” A through F can be upper or lower case Uppercase is usually preferred Lowercase is more readable for long numbers

10 Integer types There are four integer types byte – occupies one byte (surprise!) Can hold numbers in range –128 to 127 short – occupies two bytes Can hold numbers in range –32768 to int – occupies four bytes Can hold numbers up to + or – 2 billion long – occupies eight bytes Can hold numbers up to about 19 digits Literals are written with an L suffix: L A lowercase L can be used, but it’s a bad idea: l

11 Floating-point literals Floating-point literals are written with a decimal point: Floating-point numbers may also be written in “scientific notation”– times a power of 10 We use E to represent “times 10 to the” Example: 4.32E5 means 4.32 x 10 5 float literals are written with an F suffix Examples: 8.5F F 5.000F 4.32E5F If you don’t have the F suffix, type double is assumed

12 Floating point types There are two floating-point types float – occupies four bytes Can hold numbers in the range 3.4E38 to 1.4E-45 Accuracy is about nine digits double – occupies eight bytes Can hold numbers in the range 1.7E308 to 4.9E-324 Accuracy is seventeen or eighteen digits

13 Number “width” Numeric types are considered wider or narrower than other numeric types This is based partly on number of bytes occupied Also based on how large a number it can hold Java doesn’t mind if you assign a narrow value to a wide variable: int n = 3; Java is not happy if you assign a wide value to a narrow variable: byte b = ; // illegal

14 Widening and narrowing You can always widen (assign a narrower type to a wider type): double wide; int narrow; wide = narrow; But if you want to narrow (assign a wider type to a narrower type), you have to cast it: narrow = (int)wide; double float long int short byte

15 Casts You can convert (cast) one numeric type to another When you widen, no explicit cast is necessary But it doesn’t hurt When you narrow, an explicit cast is required This requirement is made to help avoid errors Casting tells Java that the value in the wider type will fit in the narrower type Java checks to make sure that the cast works, and gives you an error if it didn’t

16 Example casts short s = 0; int i = 0; double d = 0.0; d = i; // legal d = s; // legal i = s; //legal i = d; // illegal s = d; // illegal s = i; // illegal i = (int) d; // legal s = (short) d; // legal s = (short) i; // legal d = 3.7E20; i = 50000; // The following give // runtime errors: s = (short) i; i = (int) d;

17 The fifth integer type The primitive type char refers to a single, two-byte Unicode character There is no good reason this should be a numeric type......but characters were numbers in C You can use characters in arithmetic (they will automatically be converted to int ) char ch = 'A'; char ch2 = (char) (ch + 1); // cast result back to char System.out.println(ch + " " + ch2 + " " + (ch + 1)); A B 66 To assign a char to a byte, or a byte to a char, you must use a cast

18 Mixed types If you mix numeric types, the narrower type is automatically promoted to the wider type int narrow = 5; double wide; double anotherWide = wide + narrow; Integer division is when you divide one integer type by another; the fractional part is discarded Example: narrow = 19 / 5; // result is 3

19 Math methods Converting a double to an int just discards the fractional part: (int)17.93 is 17 (int) –17.93 is -17 double Math.floor(double) Given a double, returns (as a double ) the largest integral value not greater than the argument Math.floor(17.93) returns 17.0 Math.floor(-17.93) returns –18.0 double Math.ceil(double) Given a double, returns (as a double ) the smallest integral value not smaller than the argument Math.ceil(17.93) returns 18.0 Math.ceil(-17.93) returns –17.0

20 Method parameters When you send a message to an object with a numeric parameter, and the object needs to promote the parameter in order to use a method, it will do so Example: double twice(double n) { return 2.0 * n; } twice(5) returns 10.0 This promotion will only occur if necessary Example 2: double half(double n) { return n / 2; } int half(int n) { return n / 2; } half(25) returns 12

21 The End