Practical C Programming Chapter 11
A bit is the smallest unit of information. Normally, it is represented by the values 1 and 0.
Eight bits together form a byte, represented by the C data type char.
C uses the prefix "0x" to indicate a hexademical (base 16) number
Representation Number
Hexadecimal 0x11
Decimal 17
Octal 021
Binary 10001
1. Bit Operators
Bit operators allow the programmer to work on individual bits
The bit operators treat each bit as independent
Operator Meaning
& Bitwise and
| Bitwise or
^ Bitwise exclusive
~ Complement
<< Shift left
>> Shift right
2. The and Operator (&)
and operator
Bit1 Bit2 Bit1 & Bit2
0 0 0
0 1 0
1 0 0
1 1 1
3. Bitwise or (|)
or Operator
Bit1 Bit2 Bit1 | bit2
0 0 0
0 1 1
1 0 1
1 1 1
4. The Bitwise Exclusive or (^)
Bit1 Bit2 Bit1 ^ Bit2
0 0 0
0 1 1
1 0 1
1 1 0
5. The Ones Complement Operator (Not)
Bit ~Bit
0 1
1 0
6. The left- and Right-Shift Operators
c=0x1C 00011100
c << 1 c=0x38 00111000
c >> 2 c=0x07 00000111
7. Right-shift Details
signed char sined char unsigned cahr
Expression 9 >> 2 -8 >> 2 248 >> 2
0000 1001 1111 1000 1111 1000
Result ??00 0010 ??11 1110 ??11 1110
Fill Sign Bit (0) Sign Bit (1) Zero
Final Result 0000 0010 1111 1110 0011 1110
2 -2 62
Name Description
ERROR True if any error is set
FRAMING_ERROR A framing error occurred for this character
PARITY_ERROR Character had the wrong parity
CARRIER_LOST The carrier signal went down
CHANNEL_DOWN Power was lost on the communication device
Bit Assignments
Bit Name
0 ERROR
1 FRAMING_ERROR
2 PARITY_ERROR
3 CARRIER_LOST
4 CHANNEL_DOWN
Bit Values
Bit Binary Value Hexadecimal constant
7 1000 0000 0x80
6 0100 0000 0x40
5 0010 0000 0x20
4 0001 0000 0x10
3 0000 1000 0x08
2 0000 0100 0x04
1 0000 0010 0x02
0 0000 0001 0x01
the definition could be:
this method of defining bits is somewhat confusing. our flags can be defined as:
to test a bit, we use the & operator to mask out the bits:
to clear a bit, use not operator with mark then anded it with flag
9. Bitmapped graphics
in bitmapped graphics, each pixel on the screen is represented by a single bit in memory.
suppose we have a small graphic device -- a 16-by-16-pixel black-and-white display, using a 2-by-16 array of bytes:
let's see what we need to do to transform our x, y index to a byte_x, byte_y, bit_index, and bit
A bit is the smallest unit of information. Normally, it is represented by the values 1 and 0.
Eight bits together form a byte, represented by the C data type char.
C uses the prefix "0x" to indicate a hexademical (base 16) number
Representation Number
Hexadecimal 0x11
Decimal 17
Octal 021
Binary 10001
1. Bit Operators
Bit operators allow the programmer to work on individual bits
The bit operators treat each bit as independent
Operator Meaning
& Bitwise and
| Bitwise or
^ Bitwise exclusive
~ Complement
<< Shift left
>> Shift right
2. The and Operator (&)
and operator
Bit1 Bit2 Bit1 & Bit2
0 0 0
0 1 0
1 0 0
1 1 1
3. Bitwise or (|)
or Operator
Bit1 Bit2 Bit1 | bit2
0 0 0
0 1 1
1 0 1
1 1 1
4. The Bitwise Exclusive or (^)
Bit1 Bit2 Bit1 ^ Bit2
0 0 0
0 1 1
1 0 1
1 1 0
5. The Ones Complement Operator (Not)
Bit ~Bit
0 1
1 0
6. The left- and Right-Shift Operators
c=0x1C 00011100
c << 1 c=0x38 00111000
c >> 2 c=0x07 00000111
7. Right-shift Details
signed char sined char unsigned cahr
Expression 9 >> 2 -8 >> 2 248 >> 2
0000 1001 1111 1000 1111 1000
Result ??00 0010 ??11 1110 ??11 1110
Fill Sign Bit (0) Sign Bit (1) Zero
Final Result 0000 0010 1111 1110 0011 1110
2 -2 62
8. Setting, Clearing, and Testing Bits
communications status valueName Description
ERROR True if any error is set
FRAMING_ERROR A framing error occurred for this character
PARITY_ERROR Character had the wrong parity
CARRIER_LOST The carrier signal went down
CHANNEL_DOWN Power was lost on the communication device
Bit Assignments
Bit Name
0 ERROR
1 FRAMING_ERROR
2 PARITY_ERROR
3 CARRIER_LOST
4 CHANNEL_DOWN
Bit Values
Bit Binary Value Hexadecimal constant
7 1000 0000 0x80
6 0100 0000 0x40
5 0010 0000 0x20
4 0001 0000 0x10
3 0000 1000 0x08
2 0000 0100 0x04
1 0000 0010 0x02
0 0000 0001 0x01
the definition could be:
/* True if any error is set */
const int ERROR = 0x01;
/* A framing error occured for this character */
const int FRAMING_ERROR = 0x20;
/* Character had the wrong parity */
const int PARITY_ERROR = 0x40;
/* The carrier signal went down */
const int CARRIER_LOST = 0x08;
/* Power was lost on the communication device */
const int CHANNEL_DOWN = 0x10;
this method of defining bits is somewhat confusing. our flags can be defined as:
/* True if any error is set */
const int ERROR = (1 << 0);
/* A framing error occured for this character */
const int FRAMING_ERROR = (1 << 1);
/* Character had the wrong parity */
const int PARITY_ERROR = (1 << 2);
/* The carrier signal went down */
const int CARRIER_LOST = (1 << 3);
/* Power was lost on the communication device */
const int CHANNEL_DOWN = (1 << 4);
to set a bit, use | operator
char flag = 0; /* start all flags at 0 */
flag |= CHANNEL_DOWN; /* Channel just died */
to test a bit, we use the & operator to mask out the bits:
if ((flag & ERROR) != 0) {
printf("Error flag is set\n");
} else {
printf("No error detected\n");
}
to clear a bit, use not operator with mark then anded it with flag
flag &= ~PARITY_ERROR; /* Who cares about parity */
9. Bitmapped graphics
in bitmapped graphics, each pixel on the screen is represented by a single bit in memory.
suppose we have a small graphic device -- a 16-by-16-pixel black-and-white display, using a 2-by-16 array of bytes:
let's see what we need to do to transform our x, y index to a byte_x, byte_y, bit_index, and bit
byte_y = y;
byte_x = x / 8;
bit_index = x % 8;
bit = 0x80 >> bit_index;
graphics[byte_x][byte_y] |= bit;
this algorithm can be condensed into a single macro:
#define set_bit(x,y) graphics[ (x) / 8][y] |= (0x80 >> ((x) % 8))
#include <stdio.h>
#define X_SIZE 40 /* size of array in X direction */
#define Y_SIZE 60 /* size of array in Y direction */
/*
* We use X_SIZE/8 because we pack 8 bits per byte
*/
char graphics[X_SIZE / 8][Y_SIZE]; /* the graphic data */
#define SET_BIT(x,y) (graphics[(x)/8][y] |= (0x80 >> ((x) % 8)))
int main() {
int loc; /* current location we are setting */
void print_graphics(void); /* print the data */
for (loc = 0; loc < X_SIZE; ++loc) {
SET_BIT(loc, loc);
}
print_graphics();
return 0;
}
/*****************************************************
* pint_graphics -- Prints the graphics bit array *
* as a set of X and .'s. *
****************************************************/
void print_graphics(void) {
int x; /* current X BYTE */
int y; /* current y location */
unsigned int bit; /* bit we are testing in the current byte */
for (y = 0; y < Y_SIZE; y++) {
/* Loop for each byte in the array */
for (x = 0; x < X_SIZE / 8; ++x) {
/* handle each bit */
for (bit = 0x80; bit > 0; bit = (bit >> 1)) {
if ((graphics[x][y] & bit) != 0) {
printf("X");
}
else {
printf(" ");
}
}
}
printf("\n");
}
}