24.12.2012 Views

CSC322 C Programming and UNIX - Department of Computer ...

CSC322 C Programming and UNIX - Department of Computer ...

CSC322 C Programming and UNIX - Department of Computer ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Binary Number Representation<br />

C guarantees a base 2 representation for all unsigned integer types:<br />

– Example: 16 bit representation (short on many implementations) <strong>of</strong> 42<br />

0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0<br />

2 15<br />

2 14<br />

2 13<br />

2 12<br />

2 11<br />

2 10<br />

2 9<br />

42 = 2 5 + 2 3 + 2 1 = 32 + 8 + 2<br />

– If a result <strong>of</strong> an arithmetic operation results in a value not representable by the<br />

result type, it is reduced modulo 2 n , where n is the width <strong>of</strong> the result type<br />

An unsigned number <strong>of</strong> a narrower type is converted to a wider type by adding<br />

an appropiate number <strong>of</strong> leading zeroes:<br />

– The 8 bit representation (char on many implementations) <strong>of</strong> 42 is:<br />

0 0 1 0 1 0 1 0<br />

2 7<br />

2 6<br />

2 5<br />

2 4<br />

2 3<br />

2 2<br />

2 1<br />

2 0<br />

The exact representation for signed integers is not fixed, however, positive signed<br />

integers are guaranteed to have the same representation in signed <strong>and</strong> unsigned<br />

types<br />

Stephan Schulz 228<br />

2 8<br />

2 7<br />

2 6<br />

2 5<br />

2 4<br />

2 3<br />

2 2<br />

2 1<br />

2 0

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!