>1); return 0; } ffff 0fff 0000 fff0 Negative numbers are treated with 2's comple"> >1); return 0; } ffff 0fff 0000 fff0 Negative numbers are treated with 2's comple">
logo

CuriousTab

CuriousTab

Discussion


Home C Programming Bitwise Operators Comments

  • Question
  • Assuming, integer is 2 byte, What will be the output of the program?
    #include;
    
    int main()
    {
        printf("%x\n", -1>>1);
        return 0;
    }
    


  • Options
  • A. ffff
  • B. 0fff
  • C. 0000
  • D. fff0

  • Correct Answer
  • ffff 

    Explanation
    Negative numbers are treated with 2's complement method.

    1's complement: Inverting the bits ( all 1s to 0s and all 0s to 1s)
    2's complement: Adding 1 to the result of 1's complement.
    Binary of 1(2byte)     :  0000 0000 0000 0001
    Representing -1:
    1s complement of 1(2byte)    : 1111 1111 1111 1110
    Adding 1 to 1's comp. result : 1111 1111 1111 1111
    Right shift 1bit(-1>>1): 1111 1111 1111 1111 (carry out 1)
    Hexadecimal            : f   f    f    f
    (Filled with 1s in the left side in the above step)
    Note:

    1. Fill with 1s in the left side for right shift for negative numbers.
    2. Fill with 0s in the right side for left shift for negative numbers.
    3. Fill with 0s in the left side for right shift for positive numbers.
    4. Fill with 0s in the right side for left shift for positive numbers.


    Bitwise Operators problems


    Search Results


    • 1. What will be the output of the program?
      #include<stdio.h>
      
      int main()
      {
          printf("%%%%\n");
          return 0;
      }
      

    • Options
    • A. %%%%%
    • B. %%
    • C. No output
    • D. Error
    • Discuss
    • 2. What will be the output of the program?
      #include<stdio.h>
      char *str = "char *str = %c%s%c; main(){ printf(str, 34, str, 34);}";
      
      int main()
      {
          printf(str, 34, str, 34);
          return 0;
      }
      

    • Options
    • A. char *str = "char *str = %c%s%c; main(){ printf(str, 34, str, 34);}"; main(){ printf(str, 34, str, 34);}
    • B. char *str = %c%s%c; main(){ printf(str, 34, str, 34);}
    • C. No output
    • D. Error in program
    • Discuss
    • 3. If the file 'source.txt' contains a line "Be my friend" which of the following will be the output of below program?
      #include<stdio.h>
      
      int main()
      {
          FILE *fs, *ft;
          char c[10];
          fs = fopen("source.txt", "r");
          c[0] = getc(fs);
          fseek(fs, 0, SEEK_END);
          fseek(fs, -3L, SEEK_CUR);
          fgets(c, 5, fs);
          puts(c);
          return 0;
      }
      

    • Options
    • A. friend
    • B. frien
    • C. end
    • D. Error in fseek();
    • Discuss
    • 4. What will be the output of the program?
      #include<stdio.h>
      
      int main()
      {
          char *p;
          p="%d\n";
          p++;
          p++;
          printf(p-2, 23);
          return 0;
      }
      

    • Options
    • A. 21
    • B. 23
    • C. Error
    • D. No output
    • Discuss
    • 5. What will be the output of the program?
      #include<stdio.h>
      
      int main()
      {
          printf("%c\n", ~('C'*-1));
          return 0;
      }
      

    • Options
    • A. A
    • B. B
    • C. C
    • D. D
    • Discuss
    • 6. What will be the output of the program?
      #include<stdio.h>
      
      int main()
      {
          unsigned int res;
          res = (64 >>(2+1-2)) & (~(1<<2));
          printf("%d\n", res);
          return 0;
      }
      

    • Options
    • A. 32
    • B. 64
    • C. 0
    • D. 128
    • Discuss
    • 7. What will be the output of the program?
      #include<stdio.h>
      
      int main()
      {
          char c=48;
          int i, mask=01;
          for(i=1; i<=5; i++)
          {
              printf("%c", c|mask);
              mask = mask<<1;
          }
          return 0;
      }
      

    • Options
    • A. 12400
    • B. 12480
    • C. 12500
    • D. 12556
    • Discuss
    • 8. 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
    • 9. What will be the output of the program?
      #include<stdio.h>
      
      int main()
      {
          int i=32, j=0x20, k, l, m;
          k=i|j;
          l=i&j;
          m=k^l;
          printf("%d, %d, %d, %d, %d\n", i, j, k, l, m);
          return 0;
      }
      

    • Options
    • A. 0, 0, 0, 0, 0
    • B. 0, 32, 32, 32, 32
    • C. 32, 32, 32, 32, 0
    • D. 32, 32, 32, 32, 32
    • Discuss
    • 10. If an unsigned int is 2 bytes wide then, What will be the output of the program?
      #include<stdio.h>
      
      int main()
      {
          unsigned int a=0xffff;
          ~a;
          printf("%x\n", a);
          return 0;
      }
      

    • Options
    • A. ffff
    • B. 0000
    • C. 00ff
    • D. ddfd
    • Discuss


    Comments

    Avatar
    Joisurb
    It also predicts recurrence more accurately than Adjuvant


Enter a new Comment