C Programming - Bitwise Operators
Exercise : Bitwise Operators - Point Out Correct Statements
- Bitwise Operators - General Questions
- Bitwise Operators - Find Output of Program
- Bitwise Operators - Point Out Correct Statements
- Bitwise Operators - True / False Questions
- Bitwise Operators - Yes / No Questions
1.
Which of the following statements are correct about the program?
#include<stdio.h>
int main()
{
unsigned int num;
int i;
scanf("%u", &num);
for(i=0; i<16; i++)
{
printf("%d", (num<<i & 1<<15)?1:0);
}
return 0;
}
Answer: Option
Explanation:
If we give input 4, it will print 00000000 00000100 ;
If we give input 3, it will print 00000000 00000011 ;
If we give input 511, it will print 00000001 11111111 ;
2.
Which of the following statements are correct about the program?
#include<stdio.h>
int main()
{
unsigned int num;
int c=0;
scanf("%u", &num);
for(;num;num>>=1)
{
if(num & 1)
c++;
}
printf("%d", c);
return 0;
}
Answer: Option
Explanation:
If we give input 4, it will print 1.
Binary-4 == 00000000 00000100 ; Total number of bits = 1.
If we give input 3, it will print 2.
Binary-3 == 00000000 00000011 ; Total number of bits = 2.
If we give input 511, it will print 9.
Binary-511 == 00000001 11111111 ; Total number of bits = 9.
3.
Which of the following statements are correct about the program?
#include<stdio.h>
char *fun(unsigned int num, int base);
int main()
{
char *s;
s=fun(128, 2);
s=fun(128, 16);
printf("%s\n",s);
return 0;
}
char *fun(unsigned int num, int base)
{
static char buff[33];
char *ptr = &buff[sizeof(buff)-1];
*ptr = '\0';
do
{
*--ptr = "0123456789abcdef"[num %base];
num /=base;
}while(num!=0);
return ptr;
}
4.
Which of the following statements are correct about the program?
#include<stdio.h>
int main()
{
unsigned int m[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
unsigned char n, i;
scanf("%d", &n);
for(i=0; i<=7; i++)
{
if(n & m[i])
printf("yes");
}
return 0;
}
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers