2
int a = 0b1011011011; int b = 0b1000110110; int c = 0b0101010101; int d = (a & b) ^ c; //Wrong 

Intended value ofd is 0b1010011110

I need to write d so that when the bit of c is 1, the corresponding bit in the result is the corresponding bit in b, but when the bit of c is 0, the corresponding bit in the result is the corresponding bit in a.

I've been stuck on this for awhile now and I can't seem to come up with something in one line.

3
  • The intended value of d has nine bits, but a, b, and c have ten bits. Is that a typo, or is there an implicit leading 0-bit? Commented Apr 2, 2020 at 2:22
  • (a & ~c) ^ (b & c)? Not quite right but it's a starting point. Commented Apr 2, 2020 at 2:26
  • 1
    Fixed, the typo it should be 10 bits Commented Apr 2, 2020 at 2:31

3 Answers 3

1

I had this earlier but didn't see your edit.

 int d = (c & b)^(~c & a) ; 
  • q = c & b yields b when c is 1 regardless of b.
  • p = ~c & a yields a when c is 0 regardless of a.
  • q ^ p simply preserves those bits exclusive to a or b and 0 otherwise.
Sign up to request clarification or add additional context in comments.

Comments

0

I get the feeling that this is for homework, but I'll answer anyway, since it's hard to explain this without giving away the answer.

Consider two simpler questions. Forget about the multiple bits and pretend a, b, c, and d are only one bit (since this is a bitwise operation, the logic will not change):

  1. When c is 1, d = b. When c is 0, d = 0.
    When c and b are both 1, d ends up being 1. If b or c is 0, d is 0.
    This means that d = b & c.
  2. When c is 0, d = a. When c is 1, d = 0.
    This is very similar to case #1, except c is flipped and a is replaced with b.
    Therefore, we can replace b with a, and c with ~c to get this solution.
    This means that d = a & ~c.

Now for your original question: if we take those two simpler examples, we can see that it is impossible for both of them to be 1. So if we want both rules to apply, we can just put an | between them, giving us:
d = (b & c) | (a & ~c).

Comments

0

I need to write d so that when the bit of c is 1, the corresponding bit in the result is the corresponding bit in b,

when c == 1 d = b

but when the bit of c is 0, the corresponding bit in the result is the corresponding bit in a.

when c == 0 d = a

This sounds like a job for bit masking!

I know you give these as your test data:

int a = 0b1011011011; int b = 0b1000110110; int c = 0b0101010101; int d = 0b1010011110; 

But this test data is just as good a test and easier to read. All I've done is rearrange the bit columns so that c doesn't change so often:

int a = 0b11011_01101; int b = 0b10101_00110; int c = 0b00000_11111; int d = 0b11011_00110; 

Since Java 7 we can also use underscores in numeric literals to make them a little easier on the eyes.

It should now be easy to see that c controls where d is copied from, a or b. Easy to read data is important too.

And now, some bit masks

assertTrue(b & c ==0b00000_00110); assertTrue(a & ^c==0b11011_00000); 

Or them together and you get:

int d = 0b11011_00110; assertTrue(d == (b & c) | (a & ^c)); 

Try that with either data set. Should still work.

You could xor them together as well. Doesn't matter because the c mask negation already excludes the potential for 1's on both sides. I chose or simply out of a sense of tradition.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.