57
Ch 14 Chapter 14 Bitwise Operators Objectives To be able to use the bitwise logical operators in programs To be able to use the bitwise shift operators in programs To understand how to create and use masks to manipulate bits Computer Science: A Structured Programming Approach Using C 1

Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

  • Upload
    others

  • View
    6

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

Ch 14Chapter 14Bitwise Operators

Objectives

p

❏ To be able to use the bitwise logical operators in programs❏ To be able to use the bitwise shift operators in programs❏ To understand how to create and use masks to manipulate bits

Computer Science: A Structured Programming Approach Using C 1

Page 2: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

14-1 Exact Size Integer Types

The integer types such as int and long are machineThe integer types, such as int and long, are machine dependent. In one computer, the size of int may be four bytes; in another computer it may be two bytes. f y ; p y yWhile many bitwise applications work well on machine-dependent integer types, other applications need to assure that the size is fixed. C allows us to define integer types of sizes 8, 16, 32, and 64 bits.

Computer Science: A Structured Programming Approach Using C 2

Page 3: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

Table 14-1 Fixed-size Integer Types

Computer Science: A Structured Programming Approach Using C 3

Page 4: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

14-2 Logical Bitwise Operators

The logical operators look at data as individual bits to be i l d F id d i lmanipulated. Four operators are provided to manipulate

bits: bitwise and (&), bitwise inclusive or (|), bitwise exclusive or (^) and one’s complement (~) The firstexclusive or ( ), and one s complement (~). The first three are binary operators; the one’s complement is a unary operator.y p

Bitwise and OperatorTopics discussed in this section:Bitwise and OperatorBitwise Inclusive or OperatorBitwise Exclusive or OperatorO ’ C l O

Computer Science: A Structured Programming Approach Using C 4

One’s Complement Operator

Page 5: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

Table 14-2 And Truth Table

Computer Science: A Structured Programming Approach Using C 5

Page 6: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

PROGRAM 14-1 Simple Bitwise And Demonstration

Computer Science: A Structured Programming Approach Using C 6

Page 7: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

PROGRAM 14-1 Simple Bitwise And Demonstration

Computer Science: A Structured Programming Approach Using C 7

Page 8: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

Table 14-3 Inclusive Or Truth Table

Computer Science: A Structured Programming Approach Using C 8

Page 9: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

PROGRAM 14-2 Simple Inclusive or Demonstration

Computer Science: A Structured Programming Approach Using C 9

Page 10: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

PROGRAM 14-2 Simple Inclusive or Demonstration

Computer Science: A Structured Programming Approach Using C 10

Page 11: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

Table 14-4 Exclusive Or Truth Table

Computer Science: A Structured Programming Approach Using C 11

Page 12: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

PROGRAM 14-3 Simple Exclusive or Demonstration

Computer Science: A Structured Programming Approach Using C 12

Page 13: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

PROGRAM 14-3 Simple Exclusive or Demonstration

Computer Science: A Structured Programming Approach Using C 13

Page 14: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

Table 14-5 One’s Complement Truth Table

Computer Science: A Structured Programming Approach Using C 14

Page 15: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

PROGRAM 14-4 One’s Complement

Computer Science: A Structured Programming Approach Using C 15

Page 16: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

PROGRAM 14-4 One’s Complement

Computer Science: A Structured Programming Approach Using C 16

Page 17: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

Computer Science: A Structured Programming Approach Using C 17

FIGURE 14-1 Checksum Calculation

Page 18: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

PROGRAM 14-5 Demonstrate Checksum

Computer Science: A Structured Programming Approach Using C 18

Page 19: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

PROGRAM 14-5 Demonstrate Checksum

Computer Science: A Structured Programming Approach Using C 19

Page 20: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

14-3 Shift Operators

The shift operators move bits to the right or the left. f p g fWhen applied to unsigned numbers, these operators are implementation independent. When used with signed numbers, however, the implementation is left to the discretion of the software engineer who designs the compilercompiler.

Topics discussed in this section:ShiftRotation

Topics discussed in this section:

Computer Science: A Structured Programming Approach Using C 20

Page 21: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

Computer Science: A Structured Programming Approach Using C 21

FIGURE 14-2 Shift-right Operation

Page 22: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

PROGRAM 14-6 Simple Shift-right Demonstration

Computer Science: A Structured Programming Approach Using C 22

Page 23: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

PROGRAM 14-6 Simple Shift-right Demonstration

Computer Science: A Structured Programming Approach Using C 23

Page 24: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

PROGRAM 14-6 Simple Shift-right Demonstration

Computer Science: A Structured Programming Approach Using C 24

Page 25: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

Table 14-6 Divide by Shift

Computer Science: A Structured Programming Approach Using C 25

Page 26: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

Computer Science: A Structured Programming Approach Using C 26

FIGURE 14-3 Shift-left Operation

Page 27: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

PROGRAM 14-7 Simple Shift-left Demonstration

Computer Science: A Structured Programming Approach Using C 27

Page 28: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

PROGRAM 14-7 Simple Shift-left Demonstration

Computer Science: A Structured Programming Approach Using C 28

Page 29: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

Table 14-7 Multiply by Shift

Computer Science: A Structured Programming Approach Using C 29

Page 30: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

Computer Science: A Structured Programming Approach Using C 30

FIGURE 14-4 Right and Left Rotation

Page 31: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

PROGRAM 14-8 Rotate Left and Right Test Driver

Computer Science: A Structured Programming Approach Using C 31

Page 32: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

PROGRAM 14-8 Rotate Left and Right Test Driver

Computer Science: A Structured Programming Approach Using C 32

Page 33: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

PROGRAM 14-8 Rotate Left and Right Test Driver

Computer Science: A Structured Programming Approach Using C 33

Page 34: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

14-4 Masks

In many programs, bits are used as binary flags: 0 isIn many programs, bits are used as binary flags: 0 is off, and 1 is on. To set and test the flags, we use a bit mask. A mask is a variable or constant, usually stored in a byte or short integer. The bits are numbered from the least significant bit (rightmost), starting at 0.

Creating MasksUsing Masks

Topics discussed in this section:

Computer Science: A Structured Programming Approach Using C 34

Using Masks

Page 35: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

Computer Science: A Structured Programming Approach Using C 35

FIGURE 14-5 Bit Mask in a 16-bit Integer

Page 36: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

PROGRAM 14-9 Determine Network Address

Computer Science: A Structured Programming Approach Using C 36

Page 37: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

PROGRAM 14-9 Determine Network Address

Computer Science: A Structured Programming Approach Using C 37

Page 38: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

PROGRAM 14-9 Determine Network Address

Computer Science: A Structured Programming Approach Using C 38

Page 39: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

PROGRAM 14-9 Determine Network Address

Computer Science: A Structured Programming Approach Using C 39

Page 40: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

PROGRAM 14-10 Determine Last Address in a Network

Computer Science: A Structured Programming Approach Using C 40

Page 41: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

PROGRAM 14-10 Determine Last Address in a Network

Computer Science: A Structured Programming Approach Using C 41

Page 42: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

PROGRAM 14-10 Determine Last Address in a Network

Computer Science: A Structured Programming Approach Using C 42

Page 43: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

PROGRAM 14-10 Determine Last Address in a Network

Computer Science: A Structured Programming Approach Using C 43

Page 44: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

Computer Science: A Structured Programming Approach Using C 44

FIGURE 14-6 Polynomial Coefficients

Page 45: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

Computer Science: A Structured Programming Approach Using C 45

FIGURE 14-7 Polynomial Division

Page 46: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

PROGRAM 14-11 Polynomial Division

Computer Science: A Structured Programming Approach Using C 46

Page 47: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

PROGRAM 14-11 Polynomial Division

Computer Science: A Structured Programming Approach Using C 47

Page 48: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

PROGRAM 14-11 Polynomial Division

Computer Science: A Structured Programming Approach Using C 48

Page 49: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

PROGRAM 14-11 Polynomial Division

Computer Science: A Structured Programming Approach Using C 49

Page 50: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

14-5 Software Engineering

In Chapter 12, we looked at what makes a good function. In this section, we look at how you design good programs.

Payroll Case StudyTopics discussed in this section:

Structure Chart Design

Computer Science: A Structured Programming Approach Using C 50

Page 51: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

Computer Science: A Structured Programming Approach Using C 51

FIGURE 14-8 Requirements for Payroll Case Study

Page 52: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

Computer Science: A Structured Programming Approach Using C 52

FIGURE 14-8 Requirements for Payroll Case Study (continued)

Page 53: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

Computer Science: A Structured Programming Approach Using C 53

FIGURE 14-8 Requirements for Payroll Case Study (continued)

Page 54: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

NoteGood programs start with a good structure chart design.

Computer Science: A Structured Programming Approach Using C 54

Page 55: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

Computer Science: A Structured Programming Approach Using C 55

FIGURE 14-9 Afferent, Efferent, and Transform Modules

Page 56: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

Computer Science: A Structured Programming Approach Using C 56

Table 14-8 Classification of Payroll Modules

Page 57: Ch 14Chapter 14 Bitwise Operators - Bumsoo Kim · The logical operators look at data as individual bits to be manil dF idd i lipulated. Four operators are provided to manipulate bits:

Computer Science: A Structured Programming Approach Using C 57

FIGURE 14-10 Streams