2

I have a sample problem with w=1, y=7, z=0, x = ~(w && y) | y; and the solution is x = -1, but I can't figure out why?

Here is my thought process:
(w && y) = (1 && 7) = 1
~1
1 in bits is 0000 0001
~1 in bits is 1111 1110

Not sure what to do from here.

2
  • What's 1111 1110 ored with 111? Commented May 9, 2011 at 0:56
  • 1
    So far so good. Now you have (1111 1110) | y. Commented May 9, 2011 at 0:57

4 Answers 4

6

The last step is a bitwise OR so you get:

1111 1110 | 0000 0111 = 1111 1111

which is -1.

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

Comments

1

~1 in bits is 1111 1110, 1111 1110 or 0000 0111 is 1111 1111, and 1111 1111 is -1. The most significant bit is the negative flag, and negative numbers are subtractive, I guess you could say. That's why a signed byte can hold down to -128 but only up to 127.

Comments

0

You're right that ~(w && y) gives 1111...0. The last bit of 7 is 1, so |ing this with 7 gives 1111...1, or -1.

Comments

0

First, you should be using & instead of && for bitwise operations. Second, after ~1 = 111...1110 is calculated, it is ORed with y (7) to get 1111..1111, the 2's-complement representation of -1.

1 Comment

I would not suggest that, as you do not know the intention of the code (at least I don't know what it really does) and the results of & and && are different.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.