Machine-translated by Google. Questions are authored in English.
Top

Bitwise Operators Questions and Answers

21.

On left shifting, the bits from the left are rotated and brought to the right and accommodated where there is empty space on the right?

Answer: B

Correct answer: False

C's shift operators do not rotate. On a left shift the bits leaving the top are simply discarded, and zeros are brought in at the bottom - nothing wraps around.

Rotation exists in most processors as a machine instruction but has no operator in C, and must be written out: (x << k) | (x >> (n-k)) on an unsigned type of \( n \) bits, which reintroduces at the bottom the bits the left shift discarded. Discarding rather than rotating is what makes a left shift a multiplication by two.

Please log in or register to report this question.

22.

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

 

Answer: B

Correct answer: fff8

On a 2-byte int, \( -1 \) is all ones: 0xffff. Shifting left by three discards the top three bits and brings three zeros in at the bottom:

\[ 1111\,1111\,1111\,1111_2 \ll 3 = 1111\,1111\,1111\,1000_2 = \mathtt{0xfff8} \]

The quick check is that the low three bits become zero and nothing else changes, so only the last hex digit moves, from f to 8. Note that left-shifting a negative value is undefined behaviour in C; the result shown is what implementations produce, not what the standard guarantees.

Please log in or register to report this question.

23.

In which numbering system can the binary number 1011011111000101 be easily converted to?

Answer: B

Correct answer: Hexadecimal system

Sixteen is a power of two, so each hexadecimal digit corresponds to exactly four binary digits and the conversion is a matter of grouping rather than of arithmetic.

Splitting the number from the right gives 1011 0111 1100 0101, which reads directly as B7C5. Octal shares the property with groups of three bits, but sixteen bits divide evenly into four hexadecimal digits and not into whole octal ones, which is why hex is the convention for byte-oriented data. Decimal is a power of ten and requires repeated division.

Please log in or register to report this question.

24.

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

 

Answer: A

Correct answer: It converts a number to a given base.

The function is a general base converter. It builds the string backwards from the end of the buffer, taking num % base as the next digit, looking that digit up in "0123456789abcdef", and dividing num by base until nothing remains.

Because both the digit table and the divisor come from the base parameter, it handles any base up to 16 - binary and hexadecimal are merely the two the caller happens to use. The do...while form guarantees at least one digit, so an input of zero yields "0" rather than an empty string.

Please log in or register to report this question.

25.

What will be the output of the following C code?

#include <stdio.h>
    int main()
    {
        if (7 & 8)
        printf("Honesty");
            if ((~7 & 0x000f) == 8)
                printf("is the best policy\n");
    }

 

Answer: C

Correct answer: is the best policy

Take the two tests separately. \( 7 = 0111_2 \) and \( 8 = 1000_2 \) have no bits in common, so 7 & 8 is 0 and the first printf does not run.

For the second, ~7 inverts every bit, giving ...11111000. Masking with 0x000f keeps only the low four bits, leaving \( 1000_2 = 8 \), so the test is true and is the best policy is printed alone. The mask is what makes the result independent of the width of an int, which is why it is there.

Please log in or register to report this question.

26.

Taking the bitwise OR of a number with 0 leaves the number unchanged.

Answer: B

Correct answer: True

ORing with 0 leaves each bit as it was, so 0 is the identity element for OR. The corresponding identity for AND is a value with all bits set, and for XOR it is again 0.

Please log in or register to report this question.

27.

Which statement is suitable to check 3rd (count from 0) bit is high (set) or not?

Answer: D

Correct answer: Both (1) and (2)

The two expressions build the same mask by different routes. 1<<3 shifts a single 1 into position 3, giving \( 1000_2 \); 0x08 is that same value written directly. ANDing with either isolates bit 3.

The third option is the trap: 0x03 is \( 0011_2 \), which selects bits 0 and 1 - a mask for two low bits, not for bit 3. The shift form is generally preferred in code because the bit number appears literally, so 1<<k reads as "bit k" without the reader converting hexadecimal in their head.

Please log in or register to report this question.

28.

On left shifting, the bits from the left are rotated and brought to the right and accommodated where there is empty space on the right?

Answer: B

Correct answer: False

C's shift operators do not rotate. On a left shift the bits leaving the top are simply discarded, and zeros are brought in at the bottom - nothing wraps around.

Rotation exists in most processors as a machine instruction but has no operator in C, and must be written out: (x << k) | (x >> (n-k)) on an unsigned type of \( n \) bits, which reintroduces at the bottom the bits the left shift discarded. Discarding rather than rotating is what makes a left shift a multiplication by two.

Please log in or register to report this question.

29.

What will be the output of the following C code?

#include <stdio.h>
    void main()
    {
        int x = 4;
        int *p = &x;
        int *k = p++;
        int r = p - k;
        printf("%d", r);
    }

 

Answer: C

Correct answer: 1

Pointer arithmetic is in units of the pointed-to type, not in bytes. p++ advances p by one int, and k keeps the address p held beforehand, since ++ is postfix.

The difference of two pointers is likewise expressed in elements:

\[ p - k = 1 \]

The distractor 4 is the difference in bytes on a machine with four-byte ints, which is what a beginner expects; the language deliberately hides that scaling so that pointer arithmetic works the same way for every type.

Please log in or register to report this question.

30.

Predict the output of following program.

#include <stdio.h>

int main()
{
    int x=10;
    
    x &= ~2;
    printf("x= %d",x);
    
    return 0;
}

 

Answer: B

Correct answer: x= 8

This is the clear-a-bit idiom, x &= ~mask. Build the mask first: 2 is \( 0010_2 \), so ~2 is ...11111101 - every bit set except bit 1.

ANDing forces that one position to 0 and leaves the rest untouched:

\[ 1010_2 \mathbin{\&} 1101_2 = 1000_2 = 8 \]

So the operation removes the value 2 from x. The distractor 12 is what x |= 2 would give, and 10 is what would result if the bit had not been set in the first place.

Please log in or register to report this question.

Loading…
Tags: Bitwise Operators Questions and Answers || Bitwise Operators MCQ Questions and Answers || Bitwise Operators GK Questions and Answers || Bitwise Operators GK MCQ Questions || Bitwise Operators Multiple Choice Questions and Answers || Bitwise Operators GK || GK on Bitwise Operators || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming