ECE DEPARTMENT : NOTICE BOARD

ADVISORY TO STUDENTS (Answer to "Sir, College is there today ?")

Quote :
"If egg is broken by outside force, life ends. If broken by inside force, life begins. Great things always begin from inside force, trust yourself."

Batchwise blogs: -2008 | 2009 | 2010 | 2011 | 2012

Internal Marks Sem-II : | 2011 | 2010 | 2009 |

Intro | Case 1 | Case 2 | Case 3 | Case 4 | Case 5 |

Previous Posts

Sunday, July 11, 2010

Today's C Program

In the following program, the first loop work, but the second one fails. Why ?


#include "stdio.h"
int main ( )
{
short int i ;     /* Loop counter */
signed char ch ; /* Loop counter of another kind */
/*Works*/
         for (i = 0x80; i != 0; i = ( i >> 1 ) ) {
            printf("i is %x (%d)\n", i, i);
    }
    /*fails*/
          for (ch = 0x80; ch != 0; ch = ( ch >> 1 ) ) {
             printf("ch is %x (%d)\n", ch, ch);
     }
      return (0);
}

1 comment:

  1. ANS: in the first loop we've used short int variable i which occupies 2bytes in mem so its range is -32768 to 32767 though the keyword signed is not used with loop counter i by default compiler takes it as signed one.

    so in the first for loop when the value 0x80 which is 128 in decimal is assigned since it falls in the valid positive range of short integer this will be easily accommodated in the variable i and through out the for loop this loop counter is right shifted and then checked against zero till the time this does not become zero the loop executes!!!

    hence the o/p of the first loop would be
    128
    64
    32
    16
    8
    4
    2
    1
    after 1 the loop counter when shifted right one time would become zero so the condition in for loop fails hence control comes on to the second for loop!!!

    here a signed char variable is used as loop counter when the same value 0x80 which is 128 in decimal is assigned it does not fall within the valid signed char range which is
    -128 to +127. so what happens is since char occupies one byte in memory and out of 1 byte the most significant bit of this one byte is used for sign i,e 1 for negative and 0 for positive so 128 in binary would be

    1 000 0000 this represents -128 because there's a one in msb this is a negative number and how it is -128???

    now binary value of 128 is 1000 0000 now to perform 2's complement first we need to perform 1's complement and then add 1 to the lsb of the result so
    1000 0000 /* bin value of 128 */
    0111 1111 /* 1's comp of 128 */
    1000 0000 /* 2's comp of 128 */
    so clearly when we say ch=0x80 in hex which is 128 in dec it would be interpreted by compiler as -128 so
    for the first time thru the loop the condition ch!=0 is true so the printf() is executed which would print
    ch is 80 (-128)
    now the statement ch=ch>>1 this is right shifting the ch variable by one bit position since this is a signed variable the shift will be arithmetic one and in this shift the sign bit is also shifted and preserved for ex: when -128 is shifted it would be :
    1000 0000
    1100 0000 which represents -64 because
    0100 0000 /* bin value of 64 */
    1011 1111 /* 1s comp of 64 */
    1100 0000 /* 2's comp of 64 */

    then when -64 is shifted it would become -32 and this when shifted it would become -16 and this when shifted it would be -4 and -2 and -2 when shifted it would be -1 now
    lets look at how -1 is represented in 2's complement form
    0000 0001 /* bin value of 1 */
    1111 1110 /* 1s comp of 1 */
    1111 1111 /* 2's comp of 1 */
    now when u arithmetically shift -1 it would be -1 only because the sign in msb is to be preserved!!!!
    so the loop stays on -1 for indefinite no of times!!!!!!!!!

    to solve this problem declare ch as unsigned char then range would be 0 to 255 so 128 gets easily accommodated into ch and the loop works fine!!!

    ReplyDelete