CS61B Lecture #14: Integers

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

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

Inf2C - Computer Systems Lecture 2 Data Representation

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.

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Arithmetic and Bitwise Operations on Binary Data

Binary Representations and Arithmetic

Computer System and programming in C

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Integers II. CSE 351 Autumn Instructor: Justin Hsia

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

Arithmetic and Bitwise Operations on Binary Data

Integers II. CSE 351 Autumn 2018

Lecture 3. More About C

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

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

COMP2611: Computer Organization. Data Representation

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

A Java program contains at least one class definition.

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

Chapter 6 Primitive types

Bitwise Data Manipulation. Bitwise operations More on integers

SECURE PROGRAMMING A.A. 2018/2019

Chapter Two MIPS Arithmetic

1.00 Lecture 4. Promotion

System Programming CISC 360. Floating Point September 16, 2008

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

Main Memory Organization

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Programming in C++ 5. Integral data types

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

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

Chapter 3 Basic Data Types. Lecture 3 1

Visual C# Instructor s Manual Table of Contents

T02 Tutorial Slides for Week 2

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

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

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

COMP 122/L Lecture 2. Kyle Dewey

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

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

Chapter 7. Basic Types

Declaration and Memory

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

Values, Variables, Types & Arithmetic Expressions. Agenda

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

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

CS Programming I: Primitives and Expressions

Types, Operators and Expressions

CEN 414 Java Programming

Semester Transition Point. EE 109 Unit 11 Binary Arithmetic. Binary Arithmetic ARITHMETIC

LECTURE 3 C++ Basics Part 2

CS 261 Fall Mike Lam, Professor Integer Encodings

The Design of C: A Rational Reconstruction"

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

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

Full file at

Chapter 12 Variables and Operators

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

Arithmetic type issues

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

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

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

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

COMP 110 Introduction to Programming. What did we discuss?

CS 270: Computer Organization. Integer Arithmetic Operations

CSC 1107: Structured Programming

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

The Design of C: A Rational Reconstruction

Manipulating Integers

Chapter 4. Operations on Data

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

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

More about Binary 9/6/2016

Today CISC124. Building Modular Code. Designing Methods. Designing Methods, Cont. Designing Methods, Cont. Assignment 1 due this Friday, 7pm.

Integers Sep 3, 2002

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

Chapter 2 Float Point Arithmetic. Real Numbers in Decimal Notation. Real Numbers in Decimal Notation

XC Specification. 1 Lexical Conventions. 1.1 Tokens. The specification given in this document describes version 1.0 of XC.

CS110: PROGRAMMING LANGUAGE I

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

The Design of C: A Rational Reconstruction

Chapter 2: Using Data

Computer Organization & Systems Exam I Example Questions

Lecture Topics. Announcements. Today: Integer Arithmetic (P&H ) Next: continued. Consulting hours. Introduction to Sim. Milestone #1 (due 1/26)

ESc101 : Fundamental of Computing

Transcription:

Announcement: CS61B Lecture #14: Integers Project #0 due Tuesday night. Programming contest SATURDAY! You can still sign up at https://inst.eecs.berkeley.edu/~ctest/contest/register. Test #1 will be Tuesday, 6 October 2015, 8 10PM. Test #2 will be Tuesday, 10 November 2015, 7 9PM. Today: Integer Types; Readings: A Java Reference, 6.3 4. Head First Java, Chapter 10. Readings for Upcoming Topics: Data Structures (Into Java), Chapter 1. Last modified: Mon Sep 28 12:10:19 2015 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 thereare 2 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: Mon Sep 28 12:10:19 2015 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: Mon Sep 28 12:10:19 2015 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: Mon Sep 28 12:10:19 2015 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 module 256. Last modified: Mon Sep 28 12:10:19 2015 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: Mon Sep 28 12:10:19 2015 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: Mon Sep 28 12:10:19 2015 CS61B: Lecture #14 7

Promotion Arithmetic operations (+, *,...) promote operands as needed. Promotion is just implicit conversion. For integer operations, So, if any operand is long, promote both to long. otherwise promote both to int. 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; Common example: // Defined as abyte = (byte) (abyte+1) // Assume achar is an upper-case letter char lowercasechar = (char) ( a + achar - A ); // why cast? Last modified: Mon Sep 28 12:10:19 2015 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 Whatis: (-1) >>> 29? x << n? x >> n? (x >>> 3) & ((1<<5)-1)?. Last modified: Mon Sep 28 12:10:19 2015 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 Whatis: (-1) >>> 29? = 7. x << n? x >> n? (x >>> 3) & ((1<<5)-1)?. Last modified: Mon Sep 28 12:10:19 2015 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 Whatis: (-1) >>> 29? = 7. x << n? = x 2 n. x >> n? (x >>> 3) & ((1<<5)-1)?. Last modified: Mon Sep 28 12:10:19 2015 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: 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: Mon Sep 28 12:10:19 2015 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: 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: Mon Sep 28 12:10:19 2015 CS61B: Lecture #14 9