11.07.2014 Views

C programming notes - School of Physics

C programming notes - School of Physics

C programming notes - School of Physics

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

C <strong>programming</strong> <strong>notes</strong><br />

file:///F:/my_docs/web_phys2020/C<strong>programming</strong><strong>notes</strong>.html<br />

5 <strong>of</strong> 40 19/03/2007 10:06 AM<br />

computer will become very common.<br />

Binary, octal, decimal, hexadecimal<br />

Integers can be written in various bases. The common bases used with computers are binary (base-2), octal (base-8),<br />

decimal (base-10), and hexadecimal (base-16). Octal and hexadecimal are <strong>of</strong>ten useful when doing low-level<br />

<strong>programming</strong>, since they encode a binary number in a smaller amount <strong>of</strong> typing than using binary notation.<br />

The following table compares the various numbering systems (note that you can't directly specify a binary constant in<br />

the C <strong>programming</strong> language); octal constants are distinguished by a leading zero; hexadecimal constants by a leading<br />

"0x"):<br />

Decimal Binary Octal Hexadecimal<br />

--------------------------------<br />

0 0000 000 0x0<br />

1 0001 001 0x1<br />

2 0010 002 0x2<br />

3 0011 003 0x3<br />

4 0100 004 0x4<br />

5 0101 005 0x5<br />

6 0110 006 0x6<br />

7 0111 007 0x7<br />

8 1000 010 0x8<br />

9 1001 011 0x9<br />

10 1010 012 0xa<br />

11 1011 013 0xb<br />

12 1100 014 0xc<br />

13 1101 015 0xd<br />

14 1110 016 0xe<br />

15 1111 017 0xf<br />

Conversion between binary, octal, and hexadecimal is fairly trivial using the above table. For example, to convert the<br />

binary number 11100101010 to octal, break the number into groups <strong>of</strong> three bits starting from the least significant bit<br />

(i.e., 11 100 101 010), then convert each three-bit group to octal (i.e., 3452) and add a leading 0 to tell C it is octal (i.e.,<br />

03452). To convert the number to hexadecimal, break it into groups <strong>of</strong> four bits (i.e., 111 0010 1010), convert each<br />

group (i.e., 72a) and add a leading "0x" (i.e., 0x72a).<br />

To convert between octal and hexadecimal, it is probably easiest to go via binary.<br />

To convert from binary to decimal, note that n-th bit from the least-significant is worth 2 to the power n. So,<br />

11100101010<br />

\\\\\\\\\\\_ 0 x 1 = 0<br />

\\\\\\\\\\_ 1 x 2 = 2<br />

\\\\\\\\\_ 0 x 4 = 0<br />

\\\\\\\\_ 1 x 8 = 8<br />

\\\\\\\_ 0 x 16 = 0<br />

\\\\\\_ 1 x 32 = 32<br />

\\\\\_ 0 x 64 = 0<br />

\\\\_ 0 x 128 = 0<br />

\\\_ 1 x 256 = 256<br />

\\_ 1 x 512 = 512<br />

\_ 1 x 1024 = 1024<br />

Floating-point numbers<br />

Total = 1832<br />

Scientific computation <strong>of</strong>ten requires the use <strong>of</strong> floating-point numbers, e.g., 1.23. There is an IEEE standard that<br />

describes how floating-point numbers can be represented as a group <strong>of</strong> bytes. The basic idea is to allocate a certain<br />

number <strong>of</strong> bits to the exponent (base-2), and the rest to the mantissa (which has been normalised to be between 0.5 and<br />

1). Both the exponent and mantissa have sign bits.<br />

Floating-point numbers typically come in 4, 8, or 16 byte sizes. In C these are usually called "float", "double", and<br />

"long double" respectively.<br />

For any given number <strong>of</strong> bytes allocated to store a floating-point number, there is clearly a trade<strong>of</strong>f between the range<br />

<strong>of</strong> the exponent, and the precision <strong>of</strong> the mantissa: the more bits you use for the exponent, the fewer are left over for the<br />

mantissa. We will see later how this compromise has been chosen by the IEEE for the various byte sizes.

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

Saved successfully!

Ooh no, something went wrong!