0% found this document useful (0 votes)
3 views

Lecture 11 - Good programming practice

The document covers bitwise, logical, and relational operators, detailing their functions and examples. It also explains the process of hand tracing a program to identify errors and presents a case study for a program that calculates the volume, cost, customer price, and profit of wooden crates. The program design includes steps for user input, calculations, and displaying results, along with pseudocode and formulas for the calculations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture 11 - Good programming practice

The document covers bitwise, logical, and relational operators, detailing their functions and examples. It also explains the process of hand tracing a program to identify errors and presents a case study for a program that calculates the volume, cost, customer price, and profit of wooden crates. The program design includes steps for user input, calculations, and displaying results, along with pseudocode and formulas for the calculations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Week# 6

Type Conversion, Bitwise, Logical, and Relational


Operators
Dr Taimur Ahmed
Department of IT & CS, PAF-IAST
Lecture# 11
Bitwise operators, Hand tracing a program, A case
study
Bitwise Operators
Bitwise Operators
Operator Description Example

A = 00000101 (5)
Bitwise AND operator. Output is 1 when both bits are 1,
& B = 00000011 (3)
otherwise output is 0.
A&B = 00000001 (1)
A = 00000101 (5)
Bitwise OR operator. Output is 1 when any or both bits are
| B = 00000011 (3)
1 otherwise output is 0.
A|B = 00000111 (7)
A = 00000101 (5)
Bitwise XOR operator. Output is 1 when both inputs are
^ B = 00000011 (3)
different and 0 when both inputs are same.
A^B = 00000110 (6)

Bitwise NOT operator. Compliments the bits, i.e., converts A = 00000101 (5)
~
1 to 0 and 0 to 1. ~A = 11111010 (-6)

Right shift operator. Shifts bits to right by a user A = 00000101 (5)


>>
defined bits. A>>1= 00000010 (2)

Left shift operator. Shifts bits to left by a user defined A = 00000101 (5)
<<
bits. A<<1= 00001010 (10)

Lecture# 11 - Bitwise operators, Hand tracing a program, A case study | 4


Logical Operators

Operator Description Example

Called Logical AND operator. If both the operands are non- If A = 1, B = 0


&&
zero, then condition becomes true. (A && B) is false.
Called Logical OR Operator. If any of the two operands is If A = 1, B = 0
||
non-zero, then condition becomes true. (A || B) is true.
Called Logical NOT Operator. Use to reverses the logical
If A = 1, B = 0
! state of its operand. If a condition is true, then Logical
!(A && B) is true.
NOT operator will make false.

Lecture# 11 - Bitwise operators, Hand tracing a program, A case study | 5


Hand tracing a program
Hand Tracing a Program
❑ Hand trace a program: act as if you are the computer, executing a
program:
❑ step through and ‘execute’ each statement, one-by-one
❑ record the contents of variables after statement execution, using a
hand trace chart (table)
❑ Useful to locate logic or mathematical errors

Lecture# 11 - Bitwise operators, Hand tracing a program, A case study | 7


Hand Tracing a Program

Lecture# 11 - Bitwise operators, Hand tracing a program, A case study | 8


A Case Study
A Case Study
❑ A company named “General Crates Inc.” manufactures customized
wooden crates.
❑ You have been asked to write a program that calculates the following:
➢ Volume (in cubic feet)
➢ Cost
➢ Customer price
➢ Profit on any crate that company makes

Lecture# 11 - Bitwise operators, Hand tracing a program, A case study | 10


Program Design – Algorithm
❑ The program must perform following general steps:
Step 1:
Ask the user to enter the dimensions of the crate
Step 2:
Calculate:
the crate’s volume
the cost of building the crate
the customer’s charge
the profit made
Step 3:
Display the data calculated in Step 2

Lecture# 11 - Bitwise operators, Hand tracing a program, A case study | 11


Program Design – Hierarchy Chart

Lecture# 11 - Bitwise operators, Hand tracing a program, A case study | 12


Program Design – Create Dimensions

Lecture# 11 - Bitwise operators, Hand tracing a program, A case study | 13


Program Design – Calculate Volume, Cost, Price and Profit

Lecture# 11 - Bitwise operators, Hand tracing a program, A case study | 14


Program Design – Display Calculated Data

Lecture# 11 - Bitwise operators, Hand tracing a program, A case study | 15


Psuedocode
Ask the user to input the crate's length.
Ask the user to input the crate's width.
Ask the user to input the crate's height.
Calculate the crate's volume.
Calculate the cost of building the crate.
Calculate the customer's charge for the crate.
Calculate the profit made from the crate.
Display the crate's volume.
Display the cost of building the crate.
Display the customer's charge for the crate.
Display the profit made from the crate.

Lecture# 11 - Bitwise operators, Hand tracing a program, A case study | 16


Calculations
The following formulas will be used to calculate the crate’s volume, cost,
charge, and profit:

volume = length × width × height

cost = volume × 0.23

charge = volume × 0.5

profit = charge − cost

Lecture# 11 - Bitwise operators, Hand tracing a program, A case study | 17


Variables and Constants

Lecture# 11 - Bitwise operators, Hand tracing a program, A case study | 18


The Program

Lecture# 11 - Bitwise operators, Hand tracing a program, A case study | 19


The Program – Continue

Lecture# 11 - Bitwise operators, Hand tracing a program, A case study | 20


The Program – Output

Lecture# 11 - Bitwise operators, Hand tracing a program, A case study | 21

You might also like