a=9, b=3 and c=17. Your task is to perform some bitwise operations on themas given below. (5 marks)a ^ b, a << b, a | b, a & a, ~b
Question
a=9, b=3 and c=17. Your task is to perform some bitwise operations on them as given below. (5 marks)
a ^ b
a << b
a | b
a & a
~b
Solution
-
a ^ b: This operation is called bitwise XOR. It takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different. In this case, a=9 (1001 in binary) and b=3 (0011 in binary). The XOR operation would result in 10 (1010 in binary).
-
a << b: This operation is called bitwise left shift. The left operands value is moved left by the number of bits specified by the right operand. In this case, a=9 (1001 in binary) is to be left shifted by 3 bits. The result would be 72 (1001000 in binary).
-
a | b: This operation is called bitwise OR. It takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 any of the two bits is 1. In this case, a=9 (1001 in binary) and b=3 (0011 in binary). The OR operation would result in 11 (1011 in binary).
-
a & a: This operation is called bitwise AND. It takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1. In this case, both operands are a=9 (1001 in binary). The AND operation would result in 9 (1001 in binary).
-
~b: This operation is called bitwise NOT. It takes one number and inverts all bits of it. In this case, b=3 (0011 in binary). The NOT operation would result in -4 (1100 in binary). The result is negative because the highest bit in the result is a 1, indicating a negative number in the two's complement representation.
Similar Questions
int A= 5, B = 2, C = 10, ans; ans = ((A*B < C) || C%A == B);Group of answer choicesTrueFalse
Select the correct answerWhich of the following represents the bitwise AND operator?Options|^&!
Select the correct answerWhich of the following operators is used for the bitwise OR assignment in C?Options||||=||=
Assume that A = 5, B = 6, C = 10.Which of the following Expressions will be TRUE?A > B or B < CA > B and B < CA != BA + B >= CA % B == 5
The number a is 60% greater than the positive number b. Thenumber c is 45% less than a. The number c is how many times b?
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.