CS61B Lecture #14: Integers. Last modified: Wed Sep 27 15:44: CS61B: Lecture #14 1

Similar documents
CS61B Lecture #14: Integers

But first, encode deck of cards. Integer Representation. Two possible representations. Two better representations WELLESLEY CS 240 9/8/15

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Arithmetic and Bitwise Operations on Binary Data

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

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

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Inf2C - Computer Systems Lecture 2 Data Representation

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Computer System and programming in C

Arithmetic and Bitwise Operations on Binary Data

Lecture 3. More About C

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Lecture 12 Integers. Computer and Network Security 19th of December Computer Science and Engineering Department

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Binary Representations and Arithmetic

COMP2611: Computer Organization. Data Representation

Integers II. CSE 351 Autumn 2018

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

A Java program contains at least one class definition.

Chapter 6 Primitive types

Chapter Two MIPS Arithmetic

Main Memory Organization

Programming in C++ 5. Integral data types

CS 31: Introduction to Computer Systems. 03: Binary Arithmetic January 29

THE FUNDAMENTAL DATA TYPES

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

SECURE PROGRAMMING A.A. 2018/2019

1.00 Lecture 4. Promotion

Bitwise Data Manipulation. Bitwise operations More on integers

CS313D: ADVANCED PROGRAMMING LANGUAGE

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Groups of two-state devices are used to represent data in a computer. In general, we say the states are either: high/low, on/off, 1/0,...

Number System. Introduction. Decimal Numbers

2.1. Unit 2. Integer Operations (Arithmetic, Overflow, Bitwise Logic, Shifting)

COMP 122/L Lecture 2. Kyle Dewey

1010 2?= ?= CS 64 Lecture 2 Data Representation. Decimal Numbers: Base 10. Reading: FLD Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Advanced Computer Architecture-CS501

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

LAB A Translating Data to Binary

Visual C# Instructor s Manual Table of Contents

Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

IT 1204 Section 2.0. Data Representation and Arithmetic. 2009, University of Colombo School of Computing 1

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

Chapter 3 Basic Data Types. Lecture 3 1

LECTURE 3 C++ Basics Part 2

CS 115 Lecture 4. More Python; testing software. Neil Moore

Chapter 12 Variables and Operators

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Chapter 7. Basic Types

CS 31: Intro to Systems Binary Arithmetic. Kevin Webb Swarthmore College January 26, 2016

Arithmetic type issues

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

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

CSC 1107: Structured Programming

CS 270: Computer Organization. Integer Arithmetic Operations

Declaration and Memory

Manipulating Integers

Chapter 4. Operations on Data

System Programming CISC 360. Floating Point September 16, 2008

Values, Variables, Types & Arithmetic Expressions. Agenda

Types, Operators and Expressions

CS Programming I: Primitives and Expressions

More about Binary 9/6/2016

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

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

Types, Operators and Expressions

Arithmetic Operators. Portability: Printing Numbers

Arithmetic Operations

CS 64 Week 1 Lecture 1. Kyle Dewey

Full file at

Chapter 2: Using Data

The Design of C: A Rational Reconstruction"

Computer Organization & Systems Exam I Example Questions

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

ESc101 : Fundamental of Computing

JAVA Programming Fundamentals

Recap from Last Time. CSE 2021: Computer Organization. It s All about Numbers! 5/12/2011. Text Pictures Video clips Audio

Basic Types and Formatted I/O

Number Systems and Binary Arithmetic. Quantitative Analysis II Professor Bob Orr

Program Fundamentals

SIGNED AND UNSIGNED SYSTEMS

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

Operators and Expressions:

Internal representation. Bitwise operators

Bits, Bytes and Integers

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

COMP Primitive and Class Types. Yi Hong May 14, 2015

Exercises Software Development I. 03 Data Representation. Data types, range of values, internal format, literals. October 22nd, 2014

Internal representation. Bitwise operators

OBJECT ORIENTED PROGRAMMING IN JAVA

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

Internal representation. Bitwise operators

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Chapter 2. Positional number systems. 2.1 Signed number representations Signed magnitude

SOFTWARE DEVELOPMENT 1. Operators 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

5.Coding for 64-Bit Programs

Transcription:

CS61B Lecture #14: Integers Last modified: Wed Sep 27 15:44:05 2017 CS61B: Lecture #14 1

Integer Types and Literals Type Bits Signed? Literals byte 8 Yes Cast from int: (byte) 3 short 16 Yes None. Cast from int: (short) 4096 char 16 No a // (char) 97 \n // newline ((char) 10) \t // tab ((char) 8) \\ // backslash A, \101, \u0041 // == (char) 65 int 32 Yes 123 0100 // Octal for 64 0x3f, 0xffffffff // Hexadecimal 63, -1 (!) long 64 Yes 123L, 01000L, 0x3fL 1234567891011L Negative numerals are just negated (positive) literals. N bits meansthat thereare2 N integersinthe domain of the type: If signed, range of values is 2 N 1.. 2 N 1 1. If unsigned, only non-negative numbers, and range is 0..2 N 1. Last modified: Wed Sep 27 15:44:05 2017 CS61B: Lecture #14 2

Modular Arithmetic Problem: How do we handle overflow, such as occurs in 10000*10000*10000? Some languages throw an exception (Ada), some give undefined results (C, C++) Java defines the result of any arithmetic operation or conversion on integer types to wrap around modular arithmetic. That is, the next number after the largest in an integer type is the smallest (like clock arithmetic ). E.g., (byte) 128 == (byte) (127+1) == (byte) -128 In general, If the result of some arithmetic subexpression is supposed to have type T, an n-bit integer type, then we compute the real (mathematical) value, x, and yield a number, x, that is in the range of T, and that is equivalent to x modulo 2 n. (That means that x x is a multiple of 2 n.) Last modified: Wed Sep 27 15:44:05 2017 CS61B: Lecture #14 3

Modular Arithmetic: Examples (byte) (64*8) yields 0, since 512 0 = 2 2 8. (byte) (64*2) and (byte) (127+1) yield -128, since 128 ( 128) = 1 2 8. (byte) (101*99) yields 15, since 9999 15 = 39 2 8. (byte) (-30*13) yields 122, since 390 122 = 2 2 8. (char) (-1) yields 2 16 1, since 1 (2 16 1) = 1 2 16. Last modified: Wed Sep 27 15:44:05 2017 CS61B: Lecture #14 4

Why wrap around? Modular Arithmetic and Bits Java s definition is the natural one for a machine that uses binary arithmetic. For example, consider bytes (8 bits): Decimal Binary 101 1100101 99 1100011 9999 100111 00001111 9984 100111 00000000 15 00001111 In general, bit n, counting from 0 at the right, corresponds to 2 n. The bits to the left of the vertical bars therefore represent multiples of 2 8 = 256. So throwing them away is the same as arithmetic modulo 256. Last modified: Wed Sep 27 15:44:05 2017 CS61B: Lecture #14 5

Why this representation for -1? Negative numbers 1 00000001 2 + 1 11111111 2 = 0 1 00000000 2 Only 8 bits in a byte, so bit 8 falls off, leaving 0. The truncated bit is in the 2 8 place, so throwing it away gives an equal number modulo 2 8. All bits to the left of it are also divisible by 2 8. On unsigned types (char), arithmetic is the same, but we choose to represent only non-negative numbers modulo 2 16 : 1 0000000000000001 2 + 2 16 1 1111111111111111 2 = 2 16 +0 1 0000000000000000 2 Last modified: Wed Sep 27 15:44:05 2017 CS61B: Lecture #14 6

Conversion In general Java will silently convert from one type to another if this makes sense and no information is lost from value. Otherwise, cast explicitly, as in (byte) x. Hence, given byte abyte; char achar; short ashort; int anint; long along; // OK: ashort = abyte; anint = abyte; anint = ashort; anint = achar; along = anint; // Not OK, might lose information: anint = along; abyte = anint; achar = anint; ashort = anint; ashort = achar; achar = ashort; achar = abyte; // OK by special dispensation: abyte = 13; // 13 is compile-time constant abyte = 12+100 // 112 is compile-time constant Last modified: Wed Sep 27 15:44:05 2017 CS61B: Lecture #14 7

Promotion Arithmetic operations (+, *,...) promote operands as needed. Promotion is just implicit conversion. For integer operations, if any operand is long, promote both to long. otherwise promote both to int. So, abyte + 3 == (int) abyte + 3 // Type int along + 3 == along + (long) 3 // Type long A + 2 == (int) A + 2 // Type int abyte = abyte + 1 // ILLEGAL (why?) But fortunately, abyte += 1; // Defined as abyte = (byte) (abyte+1) Common example: // Assume achar is an upper-case letter char lowercasechar = (char) ( a + achar - A ); // why cast? Last modified: Wed Sep 27 15:44:05 2017 CS61B: Lecture #14 8

Bit twiddling Java (and C, C++) allow for handling integer types as sequences of bits. No conversion to bits needed: they already are. Operations and their uses: Mask Set Flip Flip all 00101100 00101100 00101100 & 10100111 10100111 ^ 10100111 ~ 10100111 00100100 10101111 10001011 01011000 Shifting: Left Arithmetic Right Logical Right 10101101 << 3 10101101 >> 3 10101100 >>> 3 01101000 11110101 00010101 What is: (-1) >>> 29? x << n? x >> n? (x >>> 3) & ((1<<5)-1)? Last modified: Wed Sep 27 15:44:05 2017 CS61B: Lecture #14 9

Bit twiddling Java (and C, C++) allow for handling integer types as sequences of bits. No conversion to bits needed: they already are. Operations and their uses: Mask Set Flip Flip all 00101100 00101100 00101100 & 10100111 10100111 ^ 10100111 ~ 10100111 00100100 10101111 10001011 01011000 Shifting: Left Arithmetic Right Logical Right 10101101 << 3 10101101 >> 3 10101100 >>> 3 01101000 11110101 00010101 What is: (-1) >>> 29? = 7. x << n? x >> n? (x >>> 3) & ((1<<5)-1)? Last modified: Wed Sep 27 15:44:05 2017 CS61B: Lecture #14 10

Bit twiddling Java (and C, C++) allow for handling integer types as sequences of bits. No conversion to bits needed: they already are. Operations and their uses: Mask Set Flip Flip all 00101100 00101100 00101100 & 10100111 10100111 ^ 10100111 ~ 10100111 00100100 10101111 10001011 01011000 Shifting: Left Arithmetic Right Logical Right 10101101 << 3 10101101 >> 3 10101100 >>> 3 01101000 11110101 00010101 What is: (-1) >>> 29? = 7. x << n? = x 2 n. x >> n? (x >>> 3) & ((1<<5)-1)? Last modified: Wed Sep 27 15:44:05 2017 CS61B: Lecture #14 11

Bit twiddling Java (and C, C++) allow for handling integer types as sequences of bits. No conversion to bits needed: they already are. Operations and their uses: Mask Set Flip Flip all 00101100 00101100 00101100 & 10100111 10100111 ^ 10100111 ~ 10100111 00100100 10101111 10001011 01011000 Shifting: Whatis: Left Arithmetic Right Logical Right 10101101 << 3 10101101 >> 3 10101100 >>> 3 01101000 11110101 00010101 (-1) >>> 29? = 7. x << n? = x 2 n. x >> n? = x/2 n (i.e., rounded down). (x >>> 3) & ((1<<5)-1)? Last modified: Wed Sep 27 15:44:05 2017 CS61B: Lecture #14 12

Bit twiddling Java (and C, C++) allow for handling integer types as sequences of bits. No conversion to bits needed: they already are. Operations and their uses: Mask Set Flip Flip all 00101100 00101100 00101100 & 10100111 10100111 ^ 10100111 ~ 10100111 00100100 10101111 10001011 01011000 Shifting: Whatis: Left Arithmetic Right Logical Right 10101101 << 3 10101101 >> 3 10101100 >>> 3 01101000 11110101 00010101 (-1) >>> 29? = 7. x << n? = x 2 n. x >> n? = x/2 n (i.e., rounded down). (x >>> 3) & ((1<<5)-1)? 5-bit integer, bits 3 7 of x. Last modified: Wed Sep 27 15:44:05 2017 CS61B: Lecture #14 13