1 of 33

The Loop Control Structure

*

1

2 of 33

Bitwise Operators

*

2

Operation

C Code

a

b

c

d

e

f

BITWISE

AND

c = a & b

0000

1111

0000

1111

1111

1111

BITWISE

OR

d = a | b

0101

1100

0100

1101

1001

1010

BITWISE

XOR

e = a ^ b

1010

1110

1010

1110

0100

0101

BITWISE

COMPLEMENT

f = ~a

1001

0111

0001

1111

1110

0110

3 of 33

Bitwise Operators

*

3

  • The output of bitwise AND is 1 if the corresponding bits of two operands are both 1. If either bit of an operand is 0, the result of corresponding bit is evaluated to 0
  • In C Programming, bitwise AND operator is denoted by &
  • 12 = 00001100 (In Binary)
  • 25 = 00011001 (In Binary)
  • Bitwise AND of 12 and 25
  • 0000 1100
  • & 0001 1001
  • ________
  • 0000 1000 = 8 (In decimal)
  • #include <stdio.h>
  • int main(){
  • int a = 12, b = 25;
  • printf("Output = %d", a & b);
  • return 0;
  • }

4 of 33

Bitwise OR Operator |

*

4

  • The output of bitwise OR is 1 if at least one of the corresponding bit of two operands is 1
  • In C Programming, bitwise OR operator is denoted by |
  • 12 = 00001100 (In Binary)
  • 25 = 00011001 (In Binary)
  • Bitwise AND of 12 and 25
  • 0000 1100
  • | 0001 1001
  • ________
  • 00011101 = 29 (In decimal)
  • #include <stdio.h>
  • int main(){
  • int a = 12, b = 25;
  • printf("Output = %d", a | b);
  • return 0;
  • }

5 of 33

Bitwise XOR Operator ^

*

5

  • #include <stdio.h>
  • int main(){
  • int a = 12, b = 25;
  • printf("Output = %d", a ^ b);
  • return 0;
  • }
  • The result of bitwise XOR operator is 1 if the corresponding bits of two operands are opposite i.e. one is 1 and the other is 0
  • In C Programming, bitwise XOR operator is denoted by ^
  • 12 = 00001100 (In Binary)
  • 25 = 00011001 (In Binary)
  • Bitwise XOR of 12 and 25
  • 00001100
  • ^ 00011001
  • ________
  • 00010101 = 21 (In decimal)

6 of 33

Bitwise Complement Operator ~

*

6

  • A unary operator that simply flips each bit of the input
  • In C Programming, bitwise complement operator is denoted by ~
  • 12 = 0000 0000 0000 0000 0000 0000 0000 1100 Bitwise complement of 12
  • ~ 0000 0000 0000 0000 0000 0000 0000 1100
  • _____________________________________
  • 1111 1111 1111 1111 1111 1111 1111 0011
  • = -13 (decimal)
  • #include <stdio.h>
  • int main(){
  • int a = 12;
  • printf("Output = %d", ~a);
  • return 0;
  • }

7 of 33

Right Shift Operator >>

*

7

  • Right shift operator shifts all bits towards right by a certain number of locations
  • Bits that “fall off” from the right most end are lost
  • Blank spaces in the leftmost positions are filled with sign bits
  • 212 = 0000 0000 0000 0000 0000 0000 1101 0100
  • 212 >> 0 = 0000 0000 0000 0000 0000 0000 1101 0100
  • 212 >> 4 = 0000 0000 0000 0000 0000 0000 0000 1101
  • 212 >> 6 = 0000 0000 0000 0000 0000 0000 0000 0011
  • 212 >> 3 = 0000 0000 0000 0000 0000 0000 0001 1010
  • Right shift by k is equivalent to integer division with 2k

8 of 33

Left Shift Operator <<

*

8

  • Left shift operator shifts all bits towards left by a certain number of locations
  • Bits that “fall off” from the left most end are lost
  • Blank spaces in the right positions are filled with 0s
  • 212 = 0000 0000 0000 0000 0000 0000 1101 0100
  • 212 << 0 = 0000 0000 0000 0000 0000 0000 1101 0100
  • 212 << 4 = 0000 0000 0000 0000 0000 1101 0100 0000
  • 212 << 6 = 0000 0000 0000 0000 0011 0101 0000 0000
  • 212 << 28 = 0100 0000 0000 0000 0000 0000 0000 0000
  • Left shift by k is equivalent to integer multiplication with 2k

9 of 33

Example use of bitwise operators

*

9

  • Can use “masks” to extract certain bits of a number
  • Suppose I want to look at the last 6 bits of a number a
  • Create a mask with only last bits set to 1 and take & with a
  • a = 0000 0000 0000 0000 0000 0001 1010 1011
  • p = 0000 0000 0000 0000 0000 0000 0000 0001
  • q = 0000 0000 0000 0000 0000 0000 0100 0000
  • m = 0000 0000 0000 0000 0000 0000 0011 1111
  • r = 0000 0000 0000 0000 0000 0000 0010 1011
  • int a = 427;
  • int p = 1;
  • int q = p << 6;
  • int m = q – 1;
  • int r = a & m;
  • printf("%d", r); // 43

10 of 33

Precedence Table with Bitwise Operators

*

10

Operators

Description

Associativity

unary + -, ++, --, type, sizeof, ~

Unary plus/minus, increment/decrement, typecast, sizeof, bitwise complement

Right to left

* / %

Arithmetic: Multiply, divide, remainder

Left to right

+ -

Arithmetic: Add, subtract

Left to right

<< >>

Bitwise left-shift, bitwise right shift

Left to right

< > >= <=

Relational operators

Left to right

== !=

Relational operators

Left to right

&

Bitwise AND

Left to right

^

Bitwise XOR

Left to right

|

Bitwise OR

Left to right

11 of 33

Precedence Table with Bitwise Operators

*

11

Operators

Description

Associativity

&&

Logical AND

Left to Right

||

Logical OR

Left to Right

? : :

Conditional

Right to Left

=

Assignment

Right to Left

12 of 33

Loops

  • It is useful to repeatedly execute a subsequence of statements based on a condition.
  • There are three methods:
    • Using a while statement
    • Using a for statement
    • Using a do-while statement

*

12

13 of 33

while

  • syntax:

while (expression)

statement

  • the expression is evaluated. If it is non-zero, statement is executed. This process is repeated.
  • The cycle continues until the expression becomes zero.

*

13

14 of 33

while

  • To count number of characters
    • int count = 0;

char ch;

ch = getchar( );

while ( ch != ‘\n’ ) {

count ++;

ch = getchar( );

}

printf(“you entered %d characters”, count);

*

14

15 of 33

while

  • To count number of characters
    • int count = 0;

char ch;

while ( (ch = getchar( ) ) != ‘\n’ )

count ++;

printf(“you entered %d characters”, count);

*

15

16 of 33

while

  • To count number of characters
    • int count = 0;

char ch;

ch = getchar( );

while ( ch != ‘\n’ ) {

count ++;

}

printf(“you entered %d characters”, count);

*

16

  • This could result into an infinite loop and as a result the program can not terminate !

17 of 33

Breaking with break;

  • To count number of characters
    • int count = 0;

char ch;

while ( 1 ) {

ch = getchar( );

if ( ch == ‘\n’ ) break;

else count ++;

}

printf(“you entered %d characters”, count);

*

17

18 of 33

for loop

  • The for statement syntax is:
    • for(expr1; expr2; expr3)

statement

  • This is equivalent to:
    • expr1;

while (expr2) {

statement

expr3; }

*

18

19 of 33

for

  • for(expr1; expr2; expr3)

statement

  • expr1 is the initialiser
  • expr2 is the test condition
  • expr3 is the increment expression

*

19

20 of 33

for

  • To read 10 integers and sum them
  • int sum=0,s;

int j;

for(j=0; j<10; j++)

{ scanf(“%d”,&s);

sum=sum+s;

}

  • j retains its value after the for loop

*

20

21 of 33

int count = 0;� char ch;� ch = getchar( ); � while ( ch != ‘\n’ ) � { count ++; ch = getchar( ); }� printf(“you entered %d characters”, count);

int count = 0;

char ch;

for (ch = getchar( ); ch != ‘\n’; count++ )

ch = getchar( );

printf(“you entered %d characters”, count);

*

21

for to replace a while

This is equivalent to

22 of 33

for

  • for(expr1; expr2; expr3) statement
  • Either expr1 or expr2 or expr3 could be empty.
  • if expr2 is empty it means true.
  • for(;;) statement 🡺 infinite loop; has to be broken by break;

*

22

23 of 33

Comma ,

  • comma can be used both as an operator and as a separator.
  • int j,k,l; /* , is a separator */
  • printf(“%d %d %c”, j, k, c); /* , is a separator*/
  • expr1,expr2 🡪 is an expression and has value equal to expr2
  • comma has precedence less than = (assignment).
  • Associativity is from left to right

*

23

24 of 33

Comma

  • j = (2,3); /* what is the value of j*/

It is 3

  • j = 2,3; /* what is the value of j */

It is 2 because (j=2),3;

  • j = (2,3,4,5); /* value of j ? */

It is 5 because j = (((2,3),4),5)

  • comma is often used with for loops

*

24

25 of 33

for

  • /* a[ ] is an array of 10 elements */

for(j=0, k=9; j < k; j++,k--) {

t = a[j];

a[j] = a[k];

a[k] = t;

}

  • This will reverse the order of elements in the array a

*

25

26 of 33

Do-while

  • Do it first and then test the condition
  • Syntax :
    • do

statement

while ( expression );

  • The statement is executed, then expression is evaluated. It it is true, statement is evaluated again, and so on.
  • When the expression becomes false the loop terminates.

*

26

27 of 33

Do-while

  • char ch[128];

int j = 0;

do {

ch[j ++] = getchar( );

} while(ch[j -1] != ‘\n’);

ch[j -1] = ‘\0’;

  • remember that there is ; after while( … )

*

27

28 of 33

Break and continue

  • break; breaks, continue; continues
  • Just like in switch, break; exits the loop
  • In case of nested loops like
    • while(exp1){

while(exp2) {

statement1

beak;

}

statement2

}

  • break; causes the innermost loop to be exited.

*

28

29 of 33

continue;

  • It skips the rest in the loop and continues with next iteration of the loop.
  • In case of for loop it skips the rest of the loop, but it does the increment step before continuing with next iteration.
  • The continue statement applies only to loops, not to switch.

*

29

30 of 33

continue example

  • This reads ten numbers but prints roots for only +ve numbers.
      • for( j=0; j < 10; j++) {

scanf(“%f”, &v);

if (v < 0) continue;

else {

sv = sqrt(v); /* include<math.h> */

printf(“root is %f\n”, sv);

}

}

*

30

31 of 33

Nested loops

int j, a[5];

int m;

do{

char ch;

for(j=0;j<5;j++)

scanf(“%d”, &a[j]);

m=a[0]; j =1;

while(j<5){

if(m < a[j]) m = a[j];

j ++;

}

printf(“Max is %d \n Want to enter another five ints:”,m);

ch = getchar();

}while(ch == ‘y’ || ch == ‘Y’);

*

31

32 of 33

Nested loops; sorting

/* a[ ] is an int array of 10 elements */

int j,k;

int t;

for(j=10; j >1; j--) {

for(k=0; k < j -1; k++) {

if(a[k] > a[k+1]) {

t = a[k]; a[k] = a[k+1]; a[k+1] = t;

}

}

} /* this is called bubble sort */

*

32

33 of 33

Nested loops; sorting

/* a[ ] is an int array of 10 elements */

int j,k;

int t;

for(j=10; j >1; j--)

for(k=0; k < j -1; k++)

if(a[k] > a[k+1])

t = a[k], a[k] = a[k+1], a[k+1] = t;

*

33