问题描述:

can someone explain to me why the following results in b = 13?

`int a, b, c;`

a = 1|2|4;

b = 8;

c = 2;

b |= a;

b&= ~c;

It is using binary manipultaors. (Assuming ints are 1 byte, and use Two's complement for storage, etc.)

`a = 1|2|4`

means `a = 00000001 or 00000010 or 00000100`

, which is 00000111, or 7.

`b = 8`

means `b = 00001000`

.

`c = 2`

means `c = 00000010`

.

`b |= a`

means `b = b | a`

which means `b = 00001000 or 00000111`

, which is 00001111, or 15.

`~c`

means `not c`

, which is 11111101.

`b &= ~c`

means `b = b & ~c`

, which means `b = 00001111 and 11111101`

, which is 00001101, or 13.

http://www.cs.cf.ac.uk/Dave/C/node13.html

```
a = 1|2|4
= 0b001
| 0b010
| 0b100
= 0b111
= 7
b = 8 = 0b1000
c = 2 = 0b10
b|a = 0b1000
| 0b0111
= 0b1111 = 15
~c = 0b111...1101
(b|a) & ~c = 0b00..001111
& 0b11..111101
= 0b00..001101
= 13
```

lets go into binary mode: a = 0111 (7 in decimal) b = 1000 (8) c = 0010 (2)

then we OR b with a to get b = 1111 (15) c = 0010 and ~c = 1101 finally b is anded with negated c which gives us c = 1101 (13)

The link posted by Ed is a good one. Have you analyzed the operators one step at a time? Do you fully understand what is happening at each step? Is there a particular step you are stuck on? Or are you just confused at the math overall?

hint: Convert decimal to binary and give it a shot.. maybe... just maybe you'll figure it all out by yourself

`a = 1 | 2 | 4;`

Assigns the value 7 to `a`

. This is because you are performing a bitwise `OR`

operation on the constants 1, 2 and 4. Since the binary representation of each of these is 1, 10 and 100 respectively, you get 111 which is 7.

`b |= a;`

This `OR`

s `b`

and `a`

and assigns the result to `b`

. Since `b`

's binary representation is now 111 and `a`

's binary representation is 1000 (8), you end up with 1111 or 15.

`b &= ~c;`

The `~c`

in this expression means the bitwise negation of `c`

. This essentially flips all 0's to 1's and vice versa in the binary representation of `c`

. This means `c`

switches from 10 to 111...11101.

After negating `c`

, there is a bitwise `AND`

operation between `b`

and `c`

. This means only bits that are 1 in both `b`

and `c`

remain 1, all others equal 0. Since `b`

is now 1111 and `c`

is all 1's except in the second lowest order bit, all of `b`

's bits remain 1 except the 2 bit.

The result of flipping `b`

's 2 bit is the same as if you simply subtracted 2 from its value. Since its current value is 15, and 15-2 = 13, the assignment results in `b == 13`

.

您可能感兴趣的文章：

- .net - How to do Python's zip in C#?
- python - Which is web.py killer app?
- css - jQuery - addClass or removeClass - Is there an append class?
- regex - UTF-8 characters in preg_match_all (PHP)
- svn - Cloning just a particular directory with hg?
- php - Reading a specific line from a text file
- clr - Any implementation of an Unrolled Linked List in C#?
- Finding Hudson Log Files
- Forward to a payment-gateway together with POST data using cURL (or any other PHP server side solution)
- WCF in Winforms app - is it always single-threaded?

随机阅读：

**推荐内容**-

**热点内容**-
- php - Reading a specific line from a text file
- clr - Any implementation of an Unrolled Linked List in C#?
- Finding Hudson Log Files
- Forward to a payment-gateway together with POST data using cURL (or any other PHP server side solution)
- WCF in Winforms app - is it always single-threaded?
- git svn - git svn fetch does not fetch a Subversion commit message modified after initial clone
- java me - Why I am getting the bad length exception when I am running this application?
- java - How to get string.format to complain at compile time
- ruby on rails - Trigger observer of parent class on change
- python - Issue with URL pattern in Django with webmonkey tutorial