lec4_conversions_floats

lec4_conversions_floats - Conversions and Floating Point...

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

View Full Document Right Arrow Icon
Some slides adapted from Bryant, O’Hallaron, and Nath. 1 Conversions and Floating Point Marco Gruteser 331 Computer Architecture and Assembly Language
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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;
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
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 */
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online