logo

CuriousTab

CuriousTab

Discussion


Home C Programming Bitwise Operators See What Others Are Saying!
  • Question
  • Assuming a integer 2-bytes, What will be the output of the program?
    #include<stdio.h>
    
    int main()
    {
        printf("%x\n", -1<<3);
        return 0;
    }
    


  • Options
  • A. ffff
  • B. fff8
  • C. 0 
  • D. -1

  • Correct Answer
  • fff8 

    Explanation
    The system will treat negative numbers in 2's complement method.

    Example:

    Assume the size of int is 2-bytes(16 bits). The integer value 1 is represented as given below:

    Binary of 1: 00000000 00000001 (this is for positive value of 1)

    1's complement of binary 1: 11111111 11111110
    2's complement of binary 1: 11111111 11111111

    Thy system will store '11111111 11111111' in memory to represent '-1'.

    If we do left shift (3 bits) on 11111111 11111111 it will become as given below:

    11111111 11111111 ---(left shift 3 times)---> 11111111 11111000.

    So, 11111111 11111000 ---(binary to hex)---> FF F8. (Required Answer)

    Note:

    How is the negative number obtained from 2's complement value?

    As stated above, -1 is represented as '11111111 11111111' in memory.

    So, the system will take 2's complement of '11111111 11111111' to the get the original negative value back.

    Example:

    Bit Representation of -1: 11111111 11111111

    Since the left most bit is 1, it is a negative number. Then the value is

    1's complement: 00000000 00000000
    2's complement: 00000000 00000001 (Add 1 to the above result)

    Therefore, '00000000 00000001' = 1 and the sign is negative.

    Hence the value is -1.

    More questions

    • 1. Which of the following statements correct about k used in the below statement?
      char ****k;

    • Options
    • A. k is a pointer to a pointer to a pointer to a char
    • B. k is a pointer to a pointer to a pointer to a pointer to a char
    • C. k is a pointer to a char pointer
    • D. k is a pointer to a pointer to a char
    • Discuss
    • 2. What will be the output of the program?
      #include<stdio.h>
      
      void fun(void *p);
      int i;
      
      int main()
      {
          void *vptr;
          vptr = &i;
          fun(vptr);
          return 0;
      }
      void fun(void *p)
      {
          int **q;
          q = (int**)&p;
          printf("%d\n", **q);
      }
      

    • Options
    • A. Error: cannot convert from void** to int**
    • B. Garbage value
    • C. 0
    • D. No output
    • Discuss
    • 3. What will be the output of the program?
      #include<stdio.h>
      #include<string.h>
      
      int main()
      {
          static char s[] = "Hello!";
          printf("%d\n", *(s+strlen(s)));
          return 0;
      }
      

    • Options
    • A. 8
    • B. 0
    • C. 16
    • D. Error
    • Discuss
    • 4. What will be the output of the program?
      #include<stdio.h>
      
      int main()
      {
          int i=4, j=8;
          printf("%d, %d, %d\n", i|j&j|i, i|j&&j|i, i^j);
          return 0;
      }
      

    • Options
    • A. 4, 8, 0
    • B. 1, 2, 1
    • C. 12, 1, 12
    • D. 0, 0, 0
    • Discuss
    • 5. What will you do to treat the constant 3.14 as a float?

    • Options
    • A. use float(3.14f)
    • B. use 3.14f
    • C. use f(3.14)
    • D. use (f)(3.14)
    • Discuss
    • 6. What will be the output of the program if value 25 given to scanf()?
      #include<stdio.h>
      
      int main()
      {
          int i;
          printf("%d\n", scanf("%d", &i));
          return 0;
      }
      

    • Options
    • A. 25
    • B. 2
    • C. 1
    • D. 5
    • Discuss
    • 7. In the statement expression1 >> expression2. if expression1 is a signed integer with its leftmost bit set to 1 then on right shifting it the result of the statement will vary from computer to computer

    • Options
    • A. True
    • B. False
    • Discuss
    • 8. What do the following declaration signify?
      int *ptr[30];

    • Options
    • A. ptr is a pointer to an array of 30 integer pointers.
    • B. ptr is a array of 30 pointers to integers.
    • C. ptr is a array of 30 integer pointers.
    • D. ptr is a array 30 pointers.
    • Discuss
    • 9. Range of float id -2.25e+308 to 2.25e+308

    • Options
    • A. True
    • B. False
    • Discuss
    • 10. What will be the output of the program (in Turbo C under DOS)?
      #include<stdio.h>
      
      int main()
      {
          char huge *near *far *ptr1;
          char near *far *huge *ptr2;
          char far *huge *near *ptr3;
          printf("%d, %d, %d\n", sizeof(ptr1), sizeof(ptr2), sizeof(ptr3));
          return 0;
      }
      

    • Options
    • A. 4, 4, 8
    • B. 2, 4, 4
    • C. 4, 4, 2
    • D. 2, 4, 8
    • Discuss


    Comments

    There are no comments.

Enter a new Comment