You are on page 1of 34

The Loop Control Structure

* 1
Bitwise Operators
Operation C Code a b c d e f

BITWISE c=a&b 0000 1111 0000 1111 1111 1111


AND

BITWISE d=a|b 0101 1100 0100 1101 1001 1010


OR

BITWISE e=a^b 1010 1110 1010 1110 0100 0101


XOR

BITWISE f = ~a 1001 0111 0001 1111 1110 0110


COMPLEMENT

* 2
Bitwise Operators
• 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) ■#include <stdio.h>
■25 = 00011001 (In Binary)
■int main(){
■Bitwise AND of 12 and 25
■ int a = 12, b = 25;
■ 0000 1100
■ printf("Output = %d", a & b);
■& 0001 1001
■ return 0;
■ ________
■}
■ 0000 1000 = 8 (In decimal)

* 3
Bitwise OR Operator |
• 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) ■ #include <stdio.h>
■25 = 00011001 (In Binary)

■Bitwise AND of 12 and 25


■ int main(){
■ 0000 1100 ■ int a = 12, b = 25;
■| 0001 1001
■ printf("Output = %d", a | b);
■ ________
■ 00011101 = 29 (In decimal)
■ return 0;
■ }
* 4
Bitwise XOR Operator ^
• 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) ■ #include <stdio.h>


■25 = 00011001 (In Binary)
■ int main(){
■Bitwise XOR of 12 and 25
■ int a = 12, b = 25;
■ 00001100

■^ 00011001
■ printf("Output = %d", a ^ b);
■ ________ ■ return 0;
■ 00010101 = 21 (In decimal)
■ }
* 5
Bitwise Complement Operator ~
• 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 ■ #include <stdio.h>
Bitwise complement of 12
■~ 0000 0000 0000 0000 0000 0000 0000 1100
■ int main(){
■ _____________________________________ ■ int a = 12;
■ 1111 1111 1111 1111 1111 1111 1111 0011 ■ printf("Output = %d", ~a);
■= -13 (decimal)
■ return 0;
■ }

* 6
Right Shift Operator >>
• 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

* 7
Left Shift Operator <<
• 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

* 8
Left Shift Operator <<
• 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
Example use of bitwise operators
• 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
■int a = 427;
■a = 0000 0000 0000 0000 0000 0001 1010 1011
■int p = 1;
■p = 0000 0000 0000 0000 0000 0000 0000 0001
■int q = p << 6;
■q = 0000 0000 0000 0000 0000 0000 0100 0000
■int m = q – 1;
■m = 0000 0000 0000 0000 0000 0000 0011 1111
■int r = a & m;
■r = 0000 0000 0000 0000 0000 0000 0010 1011
■printf("%d", r); // 43

* 10
Precedence Table with Bitwise Operators
Operators Description Associativity
unary + -, ++, --, type, Unary plus/minus, increment/decrement, typecast, Right to left
sizeof, ~ sizeof, bitwise complement
*/% 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
Precedence Table with Bitwise Operators
Operators Description Associativity
&& Logical AND Left to Right

|| Logical OR Left to Right


?:: Conditional Right to Left
= Assignment Right to Left

* 12
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
* 13
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.

* 14
while
■ To count number of characters
❑ int count = 0;
char ch;
ch = getchar( );
while ( ch != ‘\n’ ) {
count ++;
ch = getchar( );
*
} 15
while
■ To count number of characters
❑ int count = 0;
char ch;
while ( (ch = getchar( ) ) != ‘\n’ )
count ++;
printf(“you entered %d characters”, count);

* 16
while
■ To count number of characters
❑ int count = 0;
char ch;
ch = getchar( );
while ( ch != ‘\n’ ) {
count ++;
}
printf(“you entered %d characters”, count);

❑ This could result into an infinite loop and as a result the program can not
terminate !
* 17
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);
* 18
for loop
■ The for statement syntax is:
❑ for(expr1; expr2; expr3)
statement
■ This is equivalent to:
❑ expr1;
while (expr2) {
statement
expr3; }

* 19
for
■ for(expr1; expr2; expr3)
statement
▪ expr1 is the initialiser
▪ expr2 is the test condition
▪ expr3 is the increment expression
* 20
for
■ To read 10 integers and sum them
■ int sum=0;
int j;
for(j=0; j<10; j++)
{ scanf(“%d”, j);
sum=sum+j;
}
▪ j retains its value after the for loop
* 21
for to replace a while
int count = 0;
char ch;
ch = getchar( );
while ( ch != ‘\n’ )
{ count ++; ch = getchar( ); }
printf(“you entered %d characters”, count);
This is equivalent to
int count = 0;
char ch;
for (ch = getchar( ); ch != ‘\n’; count++ )
ch = getchar( );
printf(“you entered %d characters”, count);
* 22
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;

* 23
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 24
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
* 25
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 26
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.

* 27
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( … ) 28
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.
* 29
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.

* 30
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);
}
}
* 31
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’);

* 32
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 */

* 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;

* 34

You might also like