0

What operation does the following ‘C’ statement perform?

star = star ^ 0b00100100;

(A) Toggles bits 2 and 5 of the variable star.

(B) Clears all bits except bits 2 and 5 of the variable star.

(C) Sets all bits except bits 2 and 5 of the variable star.

(D) Multiplies value in the variable star with 0b00100100.

I'm still clueless about this. Can someone help me out?

11
  • 9
    Grumpy response: none, since that won't compile. C doesn't support a 0b prefix for binary numbers, as standard. Commented Jun 5, 2013 at 13:33
  • Really? But this question came out on my past exam paper so I assume it will work though. Commented Jun 5, 2013 at 13:34
  • What's the original value of star? Commented Jun 5, 2013 at 13:36
  • try this : tutorialspoint.com/ansi_c/c_bits_manipulation.htm Commented Jun 5, 2013 at 13:36
  • 4
    @D3FTY By the way, if you know this is the XOR operator, why don't you 1. already know what it does (it's expected from someone doing CS and stuff), 2. if you don't, why don't you google it either? Commented Jun 5, 2013 at 13:38

6 Answers 6

19

XOR operator (also called "logical addition") is defined like this:

a   b   a^b
-----------
0   0    0
0   1    1
1   0    1
1   1    0

So a^0 leaves a intact while a^1 toggles it.

For multiple-bit values, the operation is performed bitwise, i.e. between corresponding bits of the operands.

Sign up to request clarification or add additional context in comments.

Comments

8

If you know how XOR works, and you know that ^ is XOR in C, then this should be pretty simple. You should know that XOR will flip bits where 1 is set, bits 2 and 5 of 0b00100100 are set, therefore it will flip those bits.

From an "during the test" standpoint, let's say you need to prove this to yourself, you really don't need to know the initial value of star to answer the question, If you know how ^ works then just throw anything in there:

 00100100
^10101010  (star's made up value)
---------
 10001110  (star's new value)

 bit position: | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0  
               |---|---|---|---|---|---|---|---
 star's new v: | 1 | 0 | 0 | 0 | 1 | 1 | 1 | 0
               |---|---|---|---|---|---|---|---
 star's old v: | 1 | 0 | 1 | 0 | 1 | 0 | 1 | 0

Then check your answers again, did it:

(A) Toggles bits 2 and 5 of the variable star. (Yes)

(B) Clears all bits except bits 2 and 5 of the variable star. (Nope)

(C) Sets all bits except bits 2 and 5 of the variable star. (Nope)

(D) Multiplies value in the variable star with 0b00100100. (36x170 = 142? Nope)

Comments

4

It is (A) toggles bits 2 and 5.

The following is the truth table for the XOR operation:

x  y  x^y
0  0   0
1  0   1
0  1   1
1  1   0

You can see from the table that x XOR 0 = x and x XOR 1 = !x.

XOR is a bitwise operation, so it operates on individual bits. Therefore if you XOR star with some constant, it will toggle the 1 bits in the constant.

You can find some explanation e.g. here.

2 Comments

Any specific explanation to it? I still don't understand it.
@H2CO3 Well, I'm sorry if I'm dumb. But you can't expect everyone to have the same level of knowledge like you, smart alex. No offense.
3

The exclusive OR has this truth table:

A   B   A^B
-----------
1   1   0
1   0   1
0   1   1
0   0   0

We can see that if B is true (1) then A is flipped (toggled), and if it's false (0) A is left alone. So the answer is (A).

1 Comment

How would that make the answer C?
1

XOR operator returns 0 if both inputs are same otherwise returns 1 if both inputs are different.For Example the Given Truth Table :-

  • a=1 b=1 => a^b=0,
  • a=0 b=0 => a^b=0,
  • a=0 b=1 => a^b=1,
  • a=1 b=0 => a^b=1.

Comments

0

well xor is binary operator that work on bits of 2 nos.

rule of xoring:for same bit ans is 0 and for different bit ans is 1 let

a= 1 0 1 0 1 1
b= 0 1 1 0 1 0
--------------
c= 1 1 0 0 0 1
--------------

compare bit of a and b bit by bit if same put 0 else put 1 xor is basically used to find the unique in given set of duplicate no. just xor all nos. and u will get the unique one(if only single unique is present)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.