Bitwise Operators and Enumeration Types PowerPoint PPT Presentation

presentation player overlay
1 / 41
About This Presentation
Transcript and Presenter's Notes

Title: Bitwise Operators and Enumeration Types


1
Bitwise Operators and Enumeration Types
2
Outline
  • Bitwise Operators and Expressions
  • Masks
  • Software Tools Printing an int Bitwise
  • Packing and unpacking
  • Enumeration Types
  • Example

3
Bitwise Operators and Expressions
  • Bitwise Operators act on integral expressions
    represented as strings of binary digits
  • These operators are system dependent. Here we
    restrict our discussion to machines having 8-bit
    bytes, 4-byte words

4
Bitwise Operators and Expressions
  • Bitwise Operators

logical operators (unary) bitwise
complement (binary) bitwise and
(binary) bitwise exclusive or (binary)
bitwise include or shift operators (binary)
left shift ltlt (binary) right shift gtgt
5
Bitwise Operators and Expressions
  • Bitwise 's rule of precedence

() (postfix) -- (postfix) left to
right (prefix) --(prefix) !
sizeof(type) (unary) - (unary) (address)
(derefence) right to left /
left to right - left to right ltlt
gtgt left to right lt lt gt gt left to
right
6
Bitwise Operators and Expressions
  • Bitwise 's rule of precedence

! left to right left to
right left to right left to
right left to right left to
right ? right to left - /
gtgt ltlt right to
left , left to right
7
Bitwise Operators and Expressions
  • Operator one's complement operator, or bitwise
    complement operator

int a70707 The binary representation of a
is 00000000 00000001 00010100 00110011 The
expression a is the bitwise complement of a. it
has binary representation 11111111 11111110
11101011 11001100
8
Bitwise Operators and Expressions
  • Two-complement
  • int n binary representation bitwise
    complement two-complement
  • 00000000 00000111 11111111 11111000 11111111
    11111001
  • 00000000 00001000 11111111 11110111 11111111
    11111000
  • 00000000 00001001 11111111 11110110 11111111
    11110111
  • -7 11111111 11111001 00000000 00000110 00000000
    00000111

9
Bitwise Operators and Expressions
  • Bitwise Binary Logical Operators
  • a b ab ab ab
  • 0 0 0 0 0
  • 0 0 1 1
  • 0 1 0 1 1
  • 1 1 1 0 1

10
Bitwise Operators and Expressions
  • Example

int a33333, b77777 expr presentation value
a 00000000 00000000 100000010
00110101 33333 b 11111111 11111110 110100000
00101111 -77777 ab 00000000 00000000 100000000
00100101 32805 ab 11111111 11111110 01010010
00011010 -110054 ab 11111111 11111110
11010010 00111111 -77249 (ab) 00000000
00000001 00101101 11000000 77248 (ab) 0000000
0 00000001 00101101 11000000 77248
11
Bitwise Operators and Expressions
  • Left and right shift operators

expr1ltltexpr2 char c'Z' expression
representation action c 00000000 00000000
00000000 01011010 un-shifted cltlt1 00000000
00000000 00000000 10110100 left-shifted
1 cltlt4 00000000 00000000 00000101 10100000
left-shifted 4 cltlt31 00000000 00000000 00000000
00000000 left-shifted 31
12
Bitwise Operators and Expressions
  • Left and right shift operators

expr1gtgtexpr2 int a1ltlt31 unsigned
b1ltlt31 expression representation action
a 10000000 00000000 00000000
01000010 un-shifted agtgt31 11110000 00000000
00000000 00000000 right-shifted 3 b 00000000
00000000 00000000 00000000 un-shifted bgtgt3 00
010000 00000000 00000000 00000000
right-shifted 3
13
Bitwise Operators and Expressions
  • Mixed Left and right shift operators

unsigned a1 n2 expression equivalent
representation value altltbgtgt1 (altltb)gtgt1 000000000
00000010 2 altlt12ltlt3 (altlt(12))ltlt3 00000000
011111111 64 abltlt12agtgtb ((ab)ltlt(12a))gtgtb
00001100 00000000 3072
14
Masks
  • A mask is a constant or variable that is used to
    extract desired bits from another variable or
    expressions.

int i, mask1 for (i0ilt10 i) printf("d",
imask)
15
Example How to print an int bitwise
/ Bit print an int expression. / include
ltlimits.hgt void bit_print(int a) int i
int n sizeof(int) CHAR_BIT / in
limits.h / int mask 1 ltlt (n - 1)
/ mask 100...0 /
16
Example How to print an int bitwise
for (i 1 i lt n i) putchar(((a
mask) 0) ? '0' '1') a ltlt 1 if
(i CHAR_BIT 0 i lt n) putchar('
')
17
Example How to print an int bitwise
Input an integer 2 2 00000000 00000000
00000000 00000010 Input an integer 3 3
00000000 00000000 00000000 00000011 Input an
integer 5 5 00000000 00000000 00000000
00000101 Input an integer 43 43 00000000
00000000 00000000 00101011
18
include ltstdio.hgt void bit_print(int a) int
main(void) int k for ( )
printf("Input an integer ") if
(scanf("d", k) ! 1) break
printf("\n7d ", k) bit_print(k)
printf("\n\n") printf("\nBye!\n\n")
return 0
19
Packing and Unpacking
  • Use bitwise expressions allow for data
    compression across bye boundaries.
  • This is very useful for saving space, but can not
    even more useful in saving time.

20
/ Pack 4 characters into an int. / include
ltlimits.hgt int pack(char a, char b, char c, char
d) int p a / p will be
packed with a, b, c, d / p (p ltlt CHAR_BIT)
b p (p ltlt CHAR_BIT) c p (p ltlt
CHAR_BIT) d return p
21
/ Unpack a byte from an int. / include
ltlimits.hgt char unpack(int p, int k)
/ k 0, 1, 2, or 3 / int n
k CHAR_BIT / n 0, 8, 16, or 24
/ unsigned mask 255 /
low-order byte / mask ltlt n return ((p
mask) gtgt n)
22
Enumeration Type
  • naming a finite set

enum day sun, mon, tue, wed, thu, fri, sat
0 1 2 3 4 5 6
declaration enum day d1, d2
That means one of the days can be assign to the
enum variable d1 or d2
23
Enumeration Type
  • enum's declaration and initialization

enum suit club1, diamonds, hearts, spades
a, b,c 1 2 3
4
enum fruit apple7, pear, orange3, lemon
frt 7 8 3 4
enum vegbeet17, carrot17, corn17 veg1, veg2
enum fir, pine tree
24
includeltstdio.hgt enum day sun, mon, tue, wed,
thu, fri, sat typedef enum day day day
find_next_day(day d) day next_day switch(d) c
ase sun next_daymon break case mon
next_daytue break
25
case tue next_daywed break case wed
next_daythu break case thu next_dayfri
break case fri next_daysat
break return next_day
26
/ The game of paper, rock, scissors.
/ include ltctype.hgt / for isspace()
/ include ltstdio.hgt / for printf(), etc
/ include ltstdlib.hgt / for rand() and
srand() / include lttime.hgt / for
time() / enum p_r_s paper, rock, scissors,
game, help, instructions, quit enum
outcome win, lose, tie, error typedef enum
p_r_s p_r_s typedef enum outcome
outcome
27
outcome compare(p_r_s player_choice,
p_r_s machine_choice) void
wrt_final_status(int win_cnt, int lose_cnt) void
wrt_game_status(int win_cnt, int lose_cnt,
int tie_cnt) void wrt_help(void) void
wrt_instructions(void) void
report_and_tabulate(outcome result,
int win_cnt_ptr, int lose_cnt_ptr, int
tie_cnt_ptr) p_r_s selection_by_machine(void
) p_r_s selection_by_player(void)
28
include "p_r_s.h" outcome compare(p_r_s
player_choice, p_r_s machine_choice) outcome
result if (player_choice
machine_choice) return tie switch
(player_choice) case paper result
(machine_choice rock) ? win lose
break case rock result
(machine_choice scissors) ? win lose
break
29
case scissors result (machine_choice
paper) ? win lose break default
printf("\nPROGRAMMER ERROR Unexpected
choice!\n\n") exit(1) return
result
30
include "p_r_s.h" void report_and_tabulate(outco
me result, int win_cnt_ptr, int
lose_cnt_ptr, int tie_cnt_ptr) switch
(result) case win win_cnt_ptr
printf("27sYou win.\n", "") break
case lose lose_cnt_ptr
printf("27sYou lose.\n", "") break
31
case tie tie_cnt_ptr
printf("27sA tie.\n", "") break
default printf("\nPROGRAMMER ERROR
Unexpected result!\n\n") exit(1)
32
include "p_r_s.h" p_r_s selection_by_machine(voi
d) return ((p_r_s) (rand() 3)) p_r_s
selection_by_player(void) char c
p_r_s player_choice printf("Input p, r, or
s ") while (isspace(c getchar())) /
skip white space / switch (c)
33
case 'p' player_choice paper
break case 'r' player_choice rock
break case 's' player_choice
scissors break case 'g'
player_choice game break case 'i'
player_choice instructions break
34
case 'q' player_choice quit
break default player_choice help
break return player_choice
35
include "p_r_s.h" void wrt_final_status(int
win_cnt, int lose_cnt) if (win_cnt gt
lose_cnt) printf("CONGRATULATIONS - You
won!\n\n") else if (win_cnt lose_cnt)
printf("A DRAW - You tied!\n\n") else
printf("SORRY - You lost!\n\n")
36
void wrt_game_status(int win_cnt, int lose_cnt,
int tie_cnt) printf("\ns\ns4d\ns4d\ns4
d\ns4d\n\n", "GAME STATUS", "
Win ", win_cnt, " Lose ", lose_cnt,
" Tie ", tie_cnt, " Total ",
win_cnt lose_cnt tie_cnt) void
wrt_help(void) printf("\ns\n", "The
following characters can be used for input\n"
" p for paper\n" " r for
rock\n"
37
" s for scissors\n" " g print
the game status\n" " h help, print
this list\n" " i reprint the
instructions\n" " q quit this
game\n") void wrt_instructions(void)
printf("\ns\n", "PAPER, ROCK,
SCISSORS\n" " In this game p is for
\"paper,\" r is for \"rock,\" and" " s is
for \"scissors.\"\n" " Both the player
and the machine\n" " will choose one of
p, r, or s." " If the two choices are the
same,\n"
38
" then the game is a tie. Otherwise\n"
" \"paper covers the rock\" (a win for
paper),\n" " \"rock breaks the
scissors\" (a win for rock),\n" "
\"scissors cut the paper\" (a win for
scissors).\n" "\n" " There are
other allowable inputs\n" " g for
game status (the number of wins so far),\n"
" h for help,\n" " i for
instructions (reprint these instructions),\n"
" q for quit (to quit the
game).\n" "\n" " This game is
played repeatedly until q is entered.\n"
"\n" " Good luck!\n")
39
Example Game of Paper, Rock, Scissors
PAPER, ROCK, SCISSORS In this game p is for
"paper," r is for "rock," and s is for
"scissors." Both the player and the machine
will choose one of p, r, or s. If the two
choices are the same, then the game is a tie.
Otherwise "paper covers the rock" (a
win for paper), "rock breaks the scissors"
(a win for rock), "scissors cut the paper"
(a win for scissors).
40
There are other allowable inputs g for
game status (the number of wins so far),
h for help, i for instructions
(reprint these instructions), q for quit
(to quit the game). This game is
played repeatedly until q is entered. Good
luck!
41
Input p, r, or s p A
tie. Input p, r, or s s
You win. Input p, r, or s s
You win. Input p, r, or s s
You lose. Input p, r, or s s
A tie. Input p, r, or s
s You lose.
Write a Comment
User Comments (0)
About PowerShow.com