DOC PREVIEW
Duke CPS 100E - From bits to bytes to ints

This preview shows page 1-2 out of 7 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

From bits to bytes to intsHow are data stored?How do we buffer char output?Buffer bit outputRepresenting pixelsBit masks and shiftsMary ShawCPS 10014.1From 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) > InfinityCPS 10014.2How 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?CPS 10014.3How 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 operationCPS 10014.4Buffer 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 bitsCPS 10014.5Representing 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 255Pixel 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 valuesvoid process(int pixel){ int blue = pixel & 0xff; int green = (pixel >> 8) & 0xff; int red = (pixel>> 16) & 0xff;}CPS 10014.6Bit masks and shiftsvoid 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 orCPS 10014.7Mary ShawComputer technology has become pervasive, so technical decisions affect many people who do not understand the technologies and their implications. Like all technologies, computing is not inherently good or bad -- individual computer applications and the uses of their results must be evaluated in terms of community standards. Of course, those standards evolve over time, often in response to the effects of


View Full Document

Duke CPS 100E - From bits to bytes to ints

Documents in this Course
Topics

Topics

9 pages

Lecture

Lecture

3 pages

Notes

Notes

2 pages

Hashing

Hashing

19 pages

Lecture

Lecture

59 pages

Lecture

Lecture

6 pages

Lecture

Lecture

4 pages

Lecture

Lecture

20 pages

Lecture

Lecture

12 pages

Lecture

Lecture

12 pages

Lecture

Lecture

7 pages

Lecture

Lecture

8 pages

Lecture

Lecture

10 pages

Lecture

Lecture

4 pages

Notes

Notes

16 pages

Lecture

Lecture

5 pages

Lecture

Lecture

9 pages

Lecture

Lecture

4 pages

Lecture

Lecture

13 pages

Lecture

Lecture

6 pages

Lecture

Lecture

16 pages

Lecture

Lecture

5 pages

Lecture

Lecture

5 pages

Lecture

Lecture

12 pages

Lecture

Lecture

12 pages

Lecture

Lecture

10 pages

Sets

Sets

14 pages

Lecture

Lecture

9 pages

Lecture

Lecture

4 pages

Test 1

Test 1

7 pages

Load more
Download From bits to bytes to ints
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view From bits to bytes to ints and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view From bits to bytes to ints 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?