From bits to bytes to ints

Slides:



Advertisements
Similar presentations
The Binary Numbering Systems
Advertisements

Arithmetic & Logic Unit Does the calculations Everything else in the computer is there to service this unit Handles integers May handle floating point.
1 Integral Data Types in C. 2 First lecture response sent out Lots of good questions Some questions will be punted to reading when appropriate 3 statements.
Introduction to Programming with Java, for Beginners
CS 61C L02 Number Representation (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
Professor Jennifer Rexford COS 217
Bitwise Operations CSE 2451 Rong Shi. Working with bits – int values Decimal (not a power of two – used for human readability) – No preceding label –
Binary numbers and arithmetic. ADDITION Addition (decimal)
1 CS/COE0447 Computer Organization & Assembly Language Pre-Chapter 2.
1 The Design of C: A Rational Reconstruction Jennifer Rexford.
Compsci 100, Fall ’.1 Views of programming l Writing code from the method/function view is pretty similar across languages  Organizing methods.
CompSci 100 Prog Design and Analysis II
1 Homework Turn in HW2 tonight HW3 is on-line already Questions?
Bit Manipulation when every bit counts. Questions on Bit Manipulation l what is the motivation for bit manipulation l what is the binary, hexadecimal,
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
Bits and Bytes. BITWISE OPERATORS Recall boolean logical operators in Java… boolean logical operators: &, |, ^ not: ! Show truth tables.
Bitwise operators. Representing integers We typically think in terms of decimal (base 10) numbers.  Why?  A decimal (or base 10) number consists of.
Lecture12. Outline Binary representation of integer numbers Operations on bits –The Bitwise AND Operator –The Bitwise Inclusive-OR Operator –The Bitwise.
Bitwise operators. Representing integers We typically think in terms of decimal (base 10) numbers.  Why?  A decimal (or base 10) number consists of.
Bitwise Operators Fall 2008 Dr. David A. Gaitros
Bit Operations Horton pp Why we need to work with bits Sometimes one bit is enough to store your data: say the gender of the student (e.g. 0.
CS1372: HELPING TO PUT THE COMPUTING IN ECE CS1372 Some Basics.
CompSci 100e Program Design and Analysis II April 7, 2011 Prof. Rodger CompSci 100e, Spring p 1 h 1 2 e 1 r 1 4 s 1 * 2 7 g 3 o
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=
CSE 351 Number Representation & Operators Section 2 October 8, 2015.
CO5023 Introduction to Digital Circuits. What do we know so far? How to represent numbers (integers and floating point) in binary How to convert between.
CSE 351 Number Representation. Number Bases Any numerical value can be represented as a linear combination of powers of n, where n is an integer greater.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
1 Manipulating Information (1). 2 Outline Bit-level operations Suggested reading –2.1.7~
CompSci 100e 10.1 Binary Digits (Bits) l Yes or No l On or Off l One or Zero l
CMSC 202 Lesson 26 Miscellaneous Topics. Warmup Decide which of the following are legal statements: int a = 7; const int b = 6; int * const p1 = & a;
CompSci From bits to bytes to ints  At some level everything is stored as either a zero or a one  A bit is a binary digit a byte is a binary.
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
CSE 220 – C Programming Expressions.
Bottom up meets Top down
Week 3 - Friday CS222.
Bits, Bytes, and Integers CSE 238/2038/2138: Systems Programming
Instructor: David Ferry
Bit Operations Horton pp
The University of Adelaide, School of Computer Science
What to bring: iCard, pens/pencils (They provide the scratch paper)
CS 240 – Lecture 8 Bitwise Operations, Bit Manipulation, Type Conversion, Conditional Expression.
Views of programming Writing code from the method/function view is pretty similar across languages Organizing methods is different, organizing code is.
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
LING 388: Computers and Language
The University of Adelaide, School of Computer Science
Bits and Bytes Topics Representing information as bits
Computer Architecture & Operations I
Bits and Bytes Topics Representing information as bits
Bits and Bytes Boolean algebra Expressing in C
Bits and Bytes Topics Representing information as bits
Bits, Bytes, and Integers 2nd Lectures
Bits and Bytes Topics Representing information as bits
Bits and Bytes Topics Representing information as bits
CS/COE0447 Computer Organization & Assembly Language
Binary Numbers.
Homework Homework Continue Reading K&R Chapter 2 Questions?
Number Representation & Operators
Comp Org & Assembly Lang
Comp Org & Assembly Lang
Bitwise Operators.
EECE.2160 ECE Application Programming
Bitwise operators.
Lecture 2: Bits, Bytes, Ints
Bit Manipulations CS212.
Bit Operations Horton pp
ECE 120 Midterm 1 HKN Review Session.
From bit to byte to char to int to long
Presentation transcript:

From bits to bytes to ints At some level everything is stored as either a zero or a one A bit is a binary digit a byte is a binary term (8 bits) We should be grateful we can deal with Strings rather than sequences of 0's and 1's. We should be grateful we can deal with an int rather than the 32 bits that make an int int values are stored as two's complement numbers with 32 bits, for 64 bits use the type long, a char is 16 bits Standard in Java, different in C/C++ Facilitates addition/subtraction for int values We don't need to worry about this, except to note: Infinity + 1 = - Infinity Math.abs(-Infinity) > Infinity

How are data stored? To facilitate compression coding we need to manipulate individual bits Why do we need to read one bit? Why do we need to write one bit? When do we read 8 bits at a time? Read 32 bits at a time? We can't actually write one bit-at-a-time. We can't really write one char at a time either. Output and input are buffered, minimize memory accesses and disk accesses Why do we care about this when we talk about data structures and algorithms? Where does data come from?

How do we buffer char output? Done for us as part of InputStream and Reader classes InputStreams are for reading bytes Readers are for reading char values Why do we have both and how do they interact? Reader r = new InputStreamReader(System.in); Do we need to flush our buffers? In the past Java IO has been notoriously slow Do we care about I? About O? This is changing, and the java.nio classes help Map a file to a region in memory in one operation

Buffer bit output To buffer bit output we need to store bits in a buffer When the buffer is full, we write it. The buffer might overflow, e.g., in process of writing 10 bits to 32-bit capacity buffer that has 29 bits in it How do we access bits, add to buffer, etc.? We need to use bit operations Mask bits -- access individual bits Shift bits – to the left or to the right Bitwise AND / OR / NEGATE bits

Bit Logical Operations Work on integers types in binary (by bit) longs, ints, chars, and bytes Three binary operators And: & Or: | Exclusive Or (xor): ^ What is result of 27 & 14? 27 | 14? 27 ^ 14? a b a&b a|b a^b 1

Bit Logical Operations Need to work bit position by bit position 11011 = 27 (many leading zeros not shown) 01110 = 14 ----------- & 01010 = | 11111 = ^ 10101 = Also have unary negation (not): ~ 00000000000000000000000000011011 = 27 ~ 11111111111111111111111111100100 = -28 Use “masks” with the various operators to Set or clear bits Test bits Toggle bits (Example later) What is result of 27 & 14? 27 | 14? 27 ^ 14”

Bit Shift Operations Work on same types as logical ops One left shift and two right shifts Left shift: << 11011 = 27 27 << 2 1101100 = 108 (shifting left is like? ) Logical right shift: >>> 27 >>> 2 110 = 6 (shifting right is like? ) Arithmetic right shift: >> 11111111111111111111111111100100 = -28 -26 >> 2 11111111111111111111111111111001 = -7 11111111111111111111111111111111 = -1 -1 >>> 16 (for contrast) 00000000000000001111111111111111 = 65535

Representing pixels A pixel typically stores RGB and alpha/transparency values Each RGB is a value in the range 0 to 255 The alpha value is also in range 0 to 255 Pixel red = new Pixel(255,0,0,0); Pixel white = new Pixel(255,255,255,0); Typically store these values as int values, a picture is simply an array of int values void process(int pixel){ int blue = pixel & 0xff; int green = (pixel >> 8) & 0xff; int red = (pixel >> 16) & 0xff; }

Bit masks and shifts void process(int pixel){ int blue = pixel & 0xff; int green = (pixel >> 8) & 0xff; int red = (pixel >> 16) & 0xff; } Hexadecimal number: 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f Note that f is 15, in binary this is 1111, one less than 10000 The hex number 0xff is an 8 bit number, all ones The bitwise & operator creates an 8 bit value, 0—255 (why) 1&1 == 1, otherwise we get 0, similar to logical and Similarly we have |, bitwise or

Bit operations revisited How do we write out all of the bits of a number /** * writes the bit representation of a int * to standard out */ void bits(int val) {

Swap two ints “in place” Swap contents of two int variables without requiring extra memory Still requires three statements (same time on most machines) Replace void swap(int[] a, int j, int k){ int temp = a[j]; a[j] = a[k]; a[k] = temp; } With a[j] = a[j] ^ a[k]; a[k] = a[j] ^ a[k]; Works because x ^ x = 0, x ^ 0 = x Proof left to the student. . . Once was useful; now more of a curiosity