20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

280 Chapter 12 Operations on Bits<br />

To convert a negative number from b<strong>in</strong>ary back to decimal, first complement all of<br />

the bits, convert the result to decimal, change the sign of the result, and then subtract 1.<br />

Given this discussion about twos complement representation, the largest positive<br />

number that can be stored <strong>in</strong>to n bits is 2 n–1 –1. So <strong>in</strong> eight bits, you can store a value up<br />

to 2 7 – 1, or 127. Similarly, the smallest negative number that can be stored <strong>in</strong>to n bits is<br />

–2 n–1 , which <strong>in</strong> an eight-bit byte comes to –128. (Can you figure out why the largest<br />

positive and smallest negative values are not of the same magnitude?)<br />

On most of today’s processors, <strong>in</strong>tegers occupy four contiguous bytes, or 32 bits, <strong>in</strong><br />

the computer’s memory.The largest positive value that can, therefore, be stored <strong>in</strong>to such<br />

an <strong>in</strong>teger is 2 31 –1 or 2,147,483,647, whereas the smallest negative number that can be<br />

stored is –2,147,483,648.<br />

In Chapter 4,“Variables, Data Types, and Arithmetic Expressions,” you were <strong>in</strong>troduced<br />

to the unsigned modifier and learned that it could be used to effectively <strong>in</strong>crease<br />

the range of a variable.This is because the leftmost bit is no longer needed to store the<br />

sign of the number because you are only deal<strong>in</strong>g with positive <strong>in</strong>tegers.This “extra” bit is<br />

used to <strong>in</strong>crease the magnitude of the value stored <strong>in</strong> that variable by a factor of 2. More<br />

precisely, n bits can now be used to store values up to 2 n –1. On a mach<strong>in</strong>e that stores<br />

<strong>in</strong>tegers <strong>in</strong> 32 bits, this means that unsigned <strong>in</strong>tegers can range <strong>in</strong> value from 0 through<br />

4,294,967,296.<br />

Bit Operators<br />

Now that you have learned some prelim<strong>in</strong>aries, it’s time to discuss the various bit operators<br />

that are available.The operators that C provides for manipulat<strong>in</strong>g bits are presented<br />

<strong>in</strong> Table 12.1.<br />

Table 12.1 Bit Operators<br />

Symbol Operation<br />

&<br />

Bitwise AND<br />

| Bitwise Inclusive-OR<br />

^<br />

Bitwise Exclusive-OR<br />

~ Ones complement<br />

> Right shift<br />

All the operators listed <strong>in</strong> Table 12.1, with the exception of the ones complement operator<br />

~,are b<strong>in</strong>ary operators and as such take two operands. Bit operations can be performed<br />

on any type of <strong>in</strong>teger value <strong>in</strong> C—be it short, long, long long,and signed or<br />

unsigned—and on characters, but cannot be performed on float<strong>in</strong>g-po<strong>in</strong>t values.

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

Saved successfully!

Ooh no, something went wrong!