Sie sind auf Seite 1von 6

SuLsecrions SuLsecrions SuLsecrions SuLsecrions

Birvise Operarors
Bir Fields
Bir Fields: Fracrical Example
A nore of caurion: ForraLiliry
Exercises
Lov Level Operarors and Bir Fields Lov Level Operarors and Bir Fields Lov Level Operarors and Bir Fields Lov Level Operarors and Bir Fields
We have seen hov poinrers give us conrrol over lov level memory operarions.
Many programs (e.g. e.g. e.g. e.g. sysrems rype applicarions) musr acrually operare ar a lov level vhere
individual Lyres musr Le operared on.
NOTE: NOTE: NOTE: NOTE: The comLinarion of poinrers and Lir-level operarors makes C useful for many lov level
applicarions and can almosr replace assemLly code. (Only aLour 10 % of UNIX is assemLly code rhe
resr is C!!.)
Birvise Operarors Birvise Operarors Birvise Operarors Birvise Operarors
The Lirvise Lirvise Lirvise Lirvise operarors of C a summarised in rhe folloving raLle:



DO NOT DO NOT DO NOT DO NOT confuse & virh &&: & is Lirvise AND, && logical AND. Similarly for and .
is a unary operaror -- ir only operares on one argumenr ro righr of rhe operaror.
TaLle: TaLle: TaLle: TaLle: Birvise
operarors
& AND
OR
XOR
One's Complimenr


Lefr shifr
Righr Shifr
Fage 1 of 6 Lov Level Operarors and Bir Fields
2/19/2008 hrrp://vvv.cs.cf.ac.uk/Dave/C/node13.hrml
The shifr operarors perform appropriare shifr Ly operaror on rhe righr ro rhe operaror on rhe lefr.
The righr operaror musr Le posirive. The vacared Lirs are filled virh zero (i.e. i.e. i.e. i.e. There is NO NO NO NO vrap
around).
For example: x 2 shifrs rhe Lirs in x Ly 2 places ro rhe lefr.
So:
if x = 00000010 (Linary) or 2 (decimal)
rhen:
or 0 (decimal)
Also: if x = 00000010 (Linary) or 2 (decimal)
or 8 (decimal)
Therefore a shifr lefr is equivalenr ro a mulriplicarion Ly 2.
Similarly a shifr righr is equal ro division Ly 2
NOTE NOTE NOTE NOTE: Shifring is much fasrer rhan acrual mulriplicarion (') or division (/) Ly 2. So if you vanr fasr
mulriplicarions or division Ly 2 use shifrs use shifrs use shifrs use shifrs.
To illusrrare many poinrs of Lirvise operarors ler us vrire a funcrion, Bitcount, rhar counrs Lirs ser
ro 1 in an 8 Lir numLer (unsigned char) passed as an argumenr ro rhe funcrion.

int bitcount(unsigned char x)

{ int count;

for (count=0; x != 0; x>>=1);
if ( x & 01)
count++;
return count;
}

This funcrion illusrrares many C program poinrs:
for loop not used for simple counting operation
x x = x >> 1
for loop will repeatedly shift right x until x becomes 0
use expression evaluation of x & 01 to control if
x & 01 masks of 1st bit of x if this is 1 then count++
Bir Fields Bir Fields Bir Fields Bir Fields
Fage 2 of 6 Lov Level Operarors and Bir Fields
2/19/2008 hrrp://vvv.cs.cf.ac.uk/Dave/C/node13.hrml
Bir Fields Bir Fields Bir Fields Bir Fields allov rhe packing of dara in a srrucrure. This is especially useful vhen memory or dara
srorage is ar a premium. Typical examples:
Facking several oLjecrs inro a machine vord. e.g. e.g. e.g. e.g. 1 Lir flags can Le compacred -- SymLol
raLles in compilers.
Reading exrernal file formars -- non-srandard file formars could Le read in. E.g. E.g. E.g. E.g. 9 Lir inregers.
C lers us do rhis in a srrucrure definirion Ly purring :Lir lengrh Lir lengrh Lir lengrh Lir lengrh afrer rhe variaLle. i.e. i.e. i.e. i.e.

struct packed_struct {
unsigned int f1:1;
unsigned int f2:1;
unsigned int f3:1;
unsigned int f4:1;
unsigned int type:4;
unsigned int funny_int:9;
} pack;

Here rhe packed_struct contains 6 members: Four 1 bit flags f1..f3, a 4 bit type and
a 9 bit funny_int.
C automatically packs the above bit fields as compactly as possible, provided that
the maximum length of the field is less than or equal to the integer word length of
the computer. If this is not the case then some compilers may allow memory overlap
for the fields whilst other would store the next field in the next word (see
comments on bit fiels portability below).
Access members as usual via:
pack.type = 7;
NOTE:
Only n lower bits will be assigned to an n bit number. So type cannot take
values larger than 15 (4 bits long).
Bit fields are always converted to integer type for computation.
You are allowed to mix ``normal'' types with bit fields.
The unsigned definition is important - ensures that no bits are used as a
flag.
Bir Fields: Fracrical Example Bir Fields: Fracrical Example Bir Fields: Fracrical Example Bir Fields: Fracrical Example
Frequenrly device conrrollers (e.g. e.g. e.g. e.g. disk drives) and rhe operaring sysrem need ro communicare ar a
lov level. Device conrrollers conrain several regisrers regisrers regisrers regisrers vhich may Le packed rogerher in one inreger
(Figure 12.1).
Fage 3 of 6 Lov Level Operarors and Bir Fields
2/19/2008 hrrp://vvv.cs.cf.ac.uk/Dave/C/node13.hrml

Fig. Fig. Fig. Fig. 12.1 12.1 12.1 12.1 Example Disk Conrroller Regisrer Example Disk Conrroller Regisrer Example Disk Conrroller Regisrer Example Disk Conrroller Regisrer We could define rhis regisrer easily virh Lir fields:
struct DISK_REGISTER {
unsigned ready:1;
unsigned error_occured:1;
unsigned disk_spinning:1;
unsigned write_protect:1;
unsigned head_loaded:1;
unsigned error_code:8;
unsigned track:9;
unsigned sector:5;
unsigned command:5;
};
To access values srored ar a parricular memory address, DISK_REGISTER_MEMORY ve can assign a
poinrer of rhe aLove srrucrure ro access rhe memory via:
struct DISK_REGISTER *disk_reg = (struct DISK_REGISTER *) DISK_REGISTER_MEMORY;
The disk driver code ro access rhis is nov relarively srraighrforvard:
/* Define sector and track to start read */

disk_reg->sector = new_sector;
disk_reg->track = new_track;
disk_reg->command = READ;

/* wait until operation done, ready will be true */

while ( ! disk_reg->ready ) ;

/* check for errors */

if (disk_reg->error_occured)
{ /* interrogate disk_reg->error_code for error type */
switch (disk_reg->error_code)
......
}
A nore of caurion: ForraLiliry A nore of caurion: ForraLiliry A nore of caurion: ForraLiliry A nore of caurion: ForraLiliry
Bir fields are a convenienr vay ro express many difficulr operarions. Hovever, Lir fields do suffer
from a lack of porraLiliry Lerveen plarforms:
inregers may Le signed or unsigned
Many compilers limir rhe maximum numLer of Lirs in rhe Lir field ro rhe size of an integer
Fage 4 of 6 Lov Level Operarors and Bir Fields
2/19/2008 hrrp://vvv.cs.cf.ac.uk/Dave/C/node13.hrml
vhich may Le eirher 16-Lir or 32-Lir varieries.
Some Lir field memLers are srored lefr ro righr orhers are srored righr ro lefr in memory.
If Lir fields roo large, nexr Lir field may Le srored consecurively in memory (overlapping rhe
Loundary Lerveen memory locarions) or in rhe nexr vord of memory.
If porraLiliry of code is a premium you can use Lir shifring and masking ro achieve rhe same resulrs
Lur nor as easy ro express or read. For example:
unsigned int *disk_reg = (unsigned int *) DISK_REGISTER_MEMORY;

/* see if disk error occured */

disk_error_occured = (disk_reg & 0x40000000) >> 31;
Exercises Exercises Exercises Exercises
Exercise 12507 Exercise 12507 Exercise 12507 Exercise 12507
Wrire a funcrion rhar prinrs our an 8-Lir (unsigned char) numLer in Linary formar.
Exercise 12514 Exercise 12514 Exercise 12514 Exercise 12514
Wrire a funcrion serLirs(x,p,n,y) rhar rerurns x virh rhe n Lirs rhar Legin ar posirion p ser ro rhe
righrmosr n Lirs of an unsigned char variaLle y (leaving orher Lirs unchanged).
E.g. if x = 10101010 (170 decimal) and y = 10100111 (167 decimal) and n = 3 and p = 6 say rhen you
need ro srrip off 3 Lirs of y (111) and pur rhem in x ar posirion 10xxx010 ro ger ansver 10111010.
Your ansver should prinr our rhe resulr in Linary form (see Exercise 12.1 alrhough inpur can Le in
decimal form.
Your ourpur should Le like rhis:
x = 10101010 (binary)
y = 10100111 (binary)
setbits n = 3, p = 6 gives x = 10111010 (binary)
Exercise 12515 Exercise 12515 Exercise 12515 Exercise 12515
Wrire a funcrion rhar inverrs rhe Lirs of an unsigned char x and srores ansver in y.
Your ansver should prinr our rhe resulr in Linary form (see Exercise 12.1 alrhough inpur can Le in
decimal form.
Your ourpur should Le like rhis:
x = 10101010 (binary)
x inverted = 01010101 (binary)
Exercise 12516 Exercise 12516 Exercise 12516 Exercise 12516
Fage 5 of 6 Lov Level Operarors and Bir Fields
2/19/2008 hrrp://vvv.cs.cf.ac.uk/Dave/C/node13.hrml
Wrire a funcrion rhar rorares (NOT shifrs NOT shifrs NOT shifrs NOT shifrs) ro rhe righr Ly n Lir posirions rhe Lirs of an unsigned char
x.ie no Lirs are losr in rhis process.
Your ansver should prinr our rhe resulr in Linary form (see Exercise 12.1 alrhough inpur can Le in
decimal form.
Your ourpur should Le like rhis:
x = 10100111 (binary)
x rotated by 3 = 11110100 (binary)
Nore Nore Nore Nore: All rhe funcrions developed should Le as concise as possiLle

Dave Marshall
1/5/1999
Fage 6 of 6 Lov Level Operarors and Bir Fields
2/19/2008 hrrp://vvv.cs.cf.ac.uk/Dave/C/node13.hrml

Das könnte Ihnen auch gefallen