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 intsAt some level everything is stored as either a zero or a oneA 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 intInt values are stored as two's complement numbers with 32 bits, for 64 bits use the type long, a char is 16 bitsStandard in Java, different in C/C++Facilitates addition/subtraction for int valuesWe 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 bitWhy 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 accessesWhy 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 classesInputStreams are for reading bytesReaders are for reading char valuesWhy 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 slowDo 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 outputTo buffer bit output we need to store bits in a bufferWhen 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 itHow do we access bits, add to buffer, etc.?We need to use bit operationsMask bits -- access individual bitsShift bits – to the left or to the rightBitwise and/or/negate bitsCPS 10014.5Representing pixelsA pixel typically stores RGB and alpha/transparency valuesEach RGB is a value in the range 0 to 255The 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,fNote that f is 15, in binary this is 1111, one less than 10000The hex number 0xff is an 8 bit number, all onesThe bitwise & operator creates an 8 bit value, 0—255 (why)1&1 == 1, otherwise we get 0, similar to logical andSimilarly we have |, bitwise orCPS 10014.7Mary ShawComputer 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