From bit to byte to char to int to long

Slides:



Advertisements
Similar presentations
Introduction to Computers and Programming. Some definitions Algorithm: –A procedure for solving a problem –A sequence of discrete steps that defines such.
Advertisements

Introduction to Programming with Java, for Beginners
Computer ArchitectureFall 2008 © August 25, CS 447 – Computer Architecture Lecture 3 Computer Arithmetic (1)
Professor Jennifer Rexford COS 217
Computer ArchitectureFall 2007 © August 29, 2007 Karem Sakallah CS 447 – Computer Architecture.
CENG 311 Machine Representation/Numbers
1 CS/COE0447 Computer Organization & Assembly Language Pre-Chapter 2.
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
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
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.
Computer Organization and Assembly Language Bitwise Operators.
Software Design 8.1 A Rose by any other name…C or Java? l Why do we use Java in our courses (royal we?)  Object oriented  Large collection of libraries.
Introduction to Programming Instructor: Yong Tang Brookhaven National Laboratory Working on accelerator control (BNL Phone #)
Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo-Cordero August 27,
CompSci 100e 12.1 Is there a Science of Networks? l What kinds of networks are there? l From Bacon numbers to random graphs to Internet  From FOAF to.
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
CompSci 100e 11.1 Sorting: From Theory to Practice l Why do we study sorting?  Because we have to  Because sorting is beautiful  Example of algorithm.
CompSci Problem: finding subsets l See CodeBloat APT, requires finding sums of all subsets  Given {72, 33, 41, 57, 25} what is sum closest (not.
CSE 351 Number Representation & Operators Section 2 October 8, 2015.
Compsci 101, Fall LWoC l Review Recommender, dictionaries, files  How to create recommendations in order? food.txt  Toward a Duke eatery-recommender.
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.
CompSci 100e 12.1 Sorting: In 2 Slides l Why do people study sorting?  Because we have to  Because sorting is beautiful  Example of algorithm analysis.
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.
CPS 100, Spring Burrows Wheeler Transform l Michael Burrows and David Wheeler in 1994, BWT l By itself it is NOT a compression scheme  It’s.
23/07/2016CSE1303 Part B lecture notes 1 Introduction to computer systems Lecture B01 Lecture notes section B01.
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.
Examples (D. Schmidt et al)
From bits to bytes to ints
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Bottom up meets Top down
Top 50 Data Structures Interview Questions
Announcements Midterm2 Grades to be announced THIS WEEK
Instructor: David Ferry
Bit Operations Horton pp
March 2006 Saeid Nooshabadi
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.
Introduction to Computer Programming
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Computer Organization & Compilation Process
Bases and Representations, Memory, Pointers, Arrays, For-Loops
Arithmetic Logical Unit
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.
Memory Management Overview
Lesson 26 Miscellaneous Topics
CS/COE0447 Computer Organization & Assembly Language
COMS 161 Introduction to Computing
All assignments and information is posted on web site
Focus of the Course Object-Oriented Software Development
Announcements Final will be NEXT WEEK on August 17
Number Representation & Operators
Comp Org & Assembly Lang
CISC101 Reminders Labs start this week. Meet your TA! Get help with:
Operations and Arithmetic
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
Binary Numbers Material on Data Representation can be found in Chapter 2 of Computer Architecture (Nicholas Carter) CSIT 301 (Blum)
Computer Organization & Compilation Process
Lecture 2: Bits, Bytes, Ints
Linked lists Low-level (concrete) data structure, used to implement higher-level structures Used to implement sequences/lists (see CList in Tapestry) Basis.
Bit Operations Horton pp
Week 1 - Friday COMP 1600.
ECE 120 Midterm 1 HKN Review Session.
Introduction to C CS 3410.
Presentation transcript:

From bit to byte to char to int to long 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 (see Integer.MAX_VALUE) Math.abs(-Infinity) > Infinity

More details about bits How is 13 represented? … _0_ _0_ _1_ _1_ _0_ _1_ 24 23 22 21 20 Total is 8+4+1 = 13 What is bit representation of 32? Of 15? Of 1023? What is bit-representation of 2n - 1? What is bit-representation of 0? Of -1? Study later, but -1 is all 1’s, left-most bit determines < 0 How can we determine what bits are on? How many on? Useful in solving problems, understanding machine

Signed, unsigned, and why we care Some applications require attention to memory-use Difference between one-million bytes, chars, and int First requires a megabyte, last requires four megabytes When do we care about these differences? Memory is cheaper, faster, …. But applications expand to use it In Java a byte is signed: -128..127(how many bits?) What if we only want 0-255? (Huff, pixels, …) Must either convert negative values or use char, trade-offs? In Java a char is unsigned, 0..65,536 (how many bits?) Why is a char unsigned? Other languages like C++/C?

How are data stored? To facilitate Huffman coding we need to read/write one bit 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

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) Only if stored as an int/char, what happens with byte? 1&1 == 1, otherwise we get 0, similar to logical and Similarly we have |, bitwise or

Problem: finding subsets See CodeBloat APT, requires finding sums of all subsets Given {72, 33, 41, 57, 25} what is sum closest (not over) 100? How do we do this in general? Consider three solutions (see also SubsetSums.java) Recursively generate all sums: similar to backtracking Current value part of sum or not, two recursive calls Use technique like sieve to form all sums Why is this so fast? Alternative solution for all sums: use bit patterns to represent substs What do 10110, 10001, 00111, 00000, and 11111 represent? How do we generate sums from these representations?

From subsets to graphs with bits We’ll consider SequenceSync APT What is a “vertex” in the graph? Where are arcs? For state-0, we have {1,5,4,2} for transitions We’ll consider a graph in which vertices are sets of states Start with every possible state in our initial vertex 1 5 1 2 4 3 2

How do we search graph? Given a vertex (collection of states) how do we determine what vertex it’s connected to? Consider each transition from each state in our vertex (remember this is a set of states) This yields a new set of states/vertex 1-away from vertex What does the code look like for bfs? When do we stop? while (q.size() != 0){ TreeSet<Integer> current = q.remove(); for(int k=0; k < 4; k++){ TreeSet<Integer> next = new TreeSet<Integer>(); for(int val : current){ next.add(matrix[val][k]); } q.add(next); // if not already seen

Problems with approach? Creating sets and looking them up in map takes time This solution times out, how to improve it? Don’t represent set of states explicitly, use sequence of bits Similar to CodeBloat, advantages? Disadvantages? How do we determine when we’re done? How to store distances (how is array like a map?) Rewrite solution to be efficient using int for set Initial set is all ones, how to make this?

A Rose by any other name…C or Java? Why do we use Java in our courses (royal we?) Object oriented Large collection of libraries Safe for advanced programming and beginners Harder to shoot ourselves in the foot Why don't we use C++ (or C)? Standard libraries weak or non-existant (comparatively) Easy to make mistakes when beginning No GUIs, complicated compilation model

Why do we learn other languages? Perl, Python, PHP, mySQL, C, C++, Java, Scheme, ML, … Can we do something different in one language? Depends on what different means. In theory: no; in practice: yes What languages do you know? All of them. In what languages are you fluent? None of them In later courses why do we use C or C++? Closer to the machine, we want to understand the machine at many levels, from the abstract to the ridiculous Or at all levels of hardware and software Some problems are better suited to one language What about writing an operating system? Linux?

C++ on two slides Classes are similar to Java, compilation model is different Classes have public and private sections/areas Typically declaration in .h file and implementation in .cpp Separate interface from actual implementation Good in theory, hard to get right in practice One .cpp file compiles to one .o file To create an executable, we link .o files with libraries Hopefully someone else takes care of the details We #include rather than import, this is a preprocessing step Literally sucks in an entire header file, can take a while for standard libraries like iostream, string, etc. No abbreviation similar to java.util.*;

C++ on a second slide We don't have to call new to create objects, they can be created "on the stack" Using new creates memory "on the heap" In C++ we need to do our own garbage collection, or avoid and run out of memory (is this an issue?) Vectors are similar to ArrayLists, pointers are similar to arrays Unfortunately, C/C++ equate array with memory allocation To access via a pointer, we don't use . we use -> Streams are used for IO, iterators are used to access begin/end of collection ifstream, cout correspond to Readers and System.out

How do we read a file in C++ and Java? Scanner s = new Scanner(new File(“data.txt”)); TreeSet<String> set = new TreeSet<String>(); while (s.hasNext()){ String str = s.next(); set.add(str); } myWordsAsList = new ArrayList<String>(set); string word; set<string> unique; ifstream input(“data.txt”); while (input >> word){ unique.insert(word); myWordsAsVector = vector<string>(unique.begin(), unique.end()); What are similarities? Differences?

How do we read a file in C? FILE * file = fopen("/u/ola/data/poe.txt","r"); char buf[1024]; char ** words = (char **) malloc(5000*sizeof(char **)); int count = 0; int k; while (fscanf(file,"%s",buf) != EOF){ int found = 0; // look for word just read for(k=0; k < count; k++){ if (strcmp(buf,words[k]) == 0){ found = 1; break; } if (!found){ // not found, add to list words[count] = (char *) malloc(strlen(buf)+1); strcpy(words[count],buf); count++; What if more than 5000 words? What if string length > 1024? What if? What is complexity of this code?