lec4_conversions_floats

# lec4_conversions_floats - Conversions and Floating Point...

This preview shows pages 1–8. Sign up to view the full content.

Some slides adapted from Bryant, O’Hallaron, and Nath. 1 Conversions and Floating Point Marco Gruteser 331 Computer Architecture and Assembly Language

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Shift Operations n Left Shift: x << y q Shift bit-vector x left y positions q Throw away extra bits on left n Fill with 0 ’s on right n Right Shift: x >> y q Shift bit-vector x right y positions n Throw away extra bits on right q Logical shift n Fill with 0 ’s on left q Arithmetic shift n Replicate most significant bit on right n Undefined Behavior q Shift amount < 0 or word size 01100010 Argument x << 3 Log. >> 2 Arith. >> 2 10100010 Argument x << 3 Log. >> 2 Arith. >> 2 00010000 00011000 00011000 00010000 00101000 11101000
Today: Bits, Bytes, and Integers n Representing information as bits n Bit-level manipulations n Integers q Representation: unsigned and signed q Conversion, casting q Expanding, truncating q Addition, negation, multiplication, shifting n Summary

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Signed vs. Unsigned in C n Constants q By default are considered to be signed integers q Unsigned if have “U” as suffix 0U, 4294967259U n Casting q Explicit casting between signed & unsigned same as U2T and T2U int tx, ty; unsigned ux, uy; tx = (int) ux; uy = (unsigned) ty; q Implicit casting also occurs via assignments and procedure calls tx = ux; uy = ty;
0 TMax TMin –1 –2 0 UMax UMax – 1 TMax TMax + 1 2’s Complement Range Unsigned Range Bits remain the same – just reinterpretation n 2’s Comp. Unsigned q Ordering Inversion q Negative Big Positive

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
0 0U == unsigned -1 0 < signed -1 0U > unsigned 2147483647 -2147483648 > signed 2147483647U -2147483648 < unsigned -1 -2 > signed (unsigned) -1 -2 > unsigned 2147483647 2147483648U < unsigned 2147483647 (int) 2147483648U > signed Casting Surprises n Expression Evaluation q If there is a mix of unsigned and signed in single expression, signed values implicitly cast to unsigned q Including comparison operations < , > , == , <= , >= q Examples for W = 32: TMIN = -2,147,483,648 , TMAX = 2,147,483,647 n Constant 1 Constant 2 Relation Evaluation 0 0U -1 0 -1 0U 2147483647 -2147483647-1 2147483647U -2147483647-1 -1 -2 (unsigned)-1 -2 2147483647 2147483648U 2147483647 (int) 2147483648U
Code Security Example n Similar to code found in FreeBSD’s implementation of getpeername n There are legions of smart people trying to find vulnerabilities in programs /* Kernel memory region holding user-accessible data */ #define KSIZE 1024 char kbuf[KSIZE]; /* Copy at most maxlen bytes from kernel region to user buffer */

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This document was uploaded on 10/20/2011.

### Page1 / 30

lec4_conversions_floats - Conversions and Floating Point...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online