lec4_conversions_floats

lec4_conversions_floats - Conversions and Floating Point...

Info icon This 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
Image of page 1

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

View Full Document Right Arrow Icon
Shift Operations Left Shift: x << y Shift bit-vector x left y positions Throw away extra bits on left Fill with 0 ’s on right Right Shift: x >> y Shift bit-vector x right y positions Throw away extra bits on right Logical shift Fill with 0 ’s on left Arithmetic shift Replicate most significant bit on right Undefined Behavior 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
Image of page 2
Today: Bits, Bytes, and Integers Representing information as bits Bit-level manipulations Integers Representation: unsigned and signed Conversion, casting Expanding, truncating Addition, negation, multiplication, shifting Summary
Image of page 3

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

View Full Document Right Arrow Icon
Signed vs. Unsigned in C Constants By default are considered to be signed integers Unsigned if have “U” as suffix 0U, 4294967259U Casting Explicit casting between signed & unsigned same as U2T and T2U int tx, ty; unsigned ux, uy; tx = (int) ux; uy = (unsigned) ty; Implicit casting also occurs via assignments and procedure calls tx = ux; uy = ty;
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 2’s Comp. Unsigned Ordering Inversion Negative Big Positive
Image of page 5

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

View Full Document Right 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 Expression Evaluation If there is a mix of unsigned and signed in single expression, signed values implicitly cast to unsigned Including comparison operations < , > , == , <= , >= Examples for W = 32: TMIN = -2,147,483,648 , TMAX = 2,147,483,647 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
Image of page 6
Code Security Example Similar to code found in FreeBSD’s implementation of getpeername 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 */ int copy_from_kernel(void *user_dest, int maxlen) { /* Byte count len is minimum of buffer size and maxlen */ int len = KSIZE < maxlen ? KSIZE : maxlen; memcpy(user_dest, kbuf, len); return len; }
Image of page 7

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern