views:

228

answers:

10

for example:

unsigned int a;    // value to merge in non-masked bits
unsigned int b;    // value to merge in masked bits
unsigned int mask; // 1 where bits from b should be selected; 0 where from a.
unsigned int r;    // result of (a & ~mask) | (b & mask) goes here

r = a ^ ((a ^ b) & mask);

merges bits from two values according to the mask.

[taken from here]

In this case, I can see that it works, but I am not sure what the logic is? And I am not sure I can create my own bit operations like this from scratch. How do I start thinking in bits?

A: 

a ^ x for some x gives the result of flipping those bits in a which are set in x.

a ^ b gives you a 1 where the bits in a and b differ, a 0 where they are the same.

Setting x to (a ^ b) & mask gives the result of flipping the bits in a which are different in a and b and are set in the mask. Thus a ^ ((a ^ b) & mask) gives the result of changing, where necessary, the values of the bits which are set in the mask from the value they take in a to the value they take in b.

Paul Baker
A: 

Thinking in bits in not that hard, you just need to convert, in your head, all the values into bits and work on them a bit at a time. That sounds hard but it does get easier over time. A good first step is to start thinking of them as hex digits (4 bits at a time).

For example, let's say a is 0x13, b is 0x22 and mask is 0x0f:

a              : 0x13 : 0001 0011
b              : 0x22 : 0010 0010
---------------------------------
a^b            : 0x31 : 0011 0001
mask           : 0x0f : 0000 1111
---------------------------------
(a^b)&mask     : 0x01 : 0000 0001
a              : 0x13 : 0001 0011
---------------------------------
a^((a^b)&mask) : 0x12 : 0001 0010

This particular example is a way to combine the top four bits of a with the bottom 4 bits of b (the mask decides which bits come from a and b.

As the site says, it's an optimization of (a & ~mask) | (b & mask):

a              : 0x13 : 0001 0011
~mask          : 0xf0 : 1111 0000
---------------------------------
a & ~mask      : 0x10 : 0001 0000

b              : 0x22 : 0010 0010
mask           : 0x0f : 0000 1111
---------------------------------
b & mask       : 0x20 : 0000 0010

a & ~mask      : 0x10 : 0001 0000
b & mask       : 0x20 : 0000 0010
---------------------------------
(a & ~mask) |  : 0x12 : 0001 0010
   (b & mask)

Aside: I wouldn't be overly concerned about not understanding something on that page you linked to. There's some serious "black magic" going on there. If you really want to understand bit fiddling, start with unoptimized ways of doing it.

paxdiablo
that's 22 seconds apart, think we're quite on the same page with the idea of how a mask looks ;-)
Abel
PS: minor error: in your second step: `a^b`, the penultimate bit must be off (it's XOR, not OR).
Abel
+7  A: 

Pencil and paper helps the best in cases like this. I usually write it down:

a        = 10101110
b        = 01100011
mask     = 11110000

a ^ b    = 10101110
           01100011
           --------
x =>       11001101

x & mask = 11001101
           11110000
           --------
x =>       11000000

a ^ x    = 11000000
           10101110
           --------
x =>       01101110

(final x is your r)
I don't know if this is the result you were after, but that's what it does. Writing it out usually helps when I don't understand a bitwise operation.

Abel
Kyle Rozendo
I don't think he's asking how it works, but rather, how he could come up with such a thing
hasen j
I find XOR mainly useful to obfuscate.
Jurily
@hasen j... you are right.
Lazer
Well, perhaps it doesn't answer all questions (some parts have been answered by others), I do think it answers *"but I am not sure what the logic is"*. Writing boolean logic this way, helps understand the process (thinking in bits). But Pod takes the latter question way further, see below.
Abel
A: 

The basis for most bitwise operations (&, |, ^, ~) is Boolean algebra. Think of performing Boolean algebra to multiple Boolean values in parallel and you've got the bitwise operators. The only operators this doesn't cover are the shift operators (<<, >>), which you can think of as shifting the bits or as multiplication by powers of two. x << 3 == x * pow(2,3) and x >> 4 == (int)(x * pow(2,-4)).

outis
A: 

Break the expression down into individual bits. Consider a single bit position in the expression (a ^ b) & mask. If the mask has zero at that bit position, (a ^ b) & mask will simply give you zero. Any bit xor'ed with zero will remain unchanged, so a ^ (a ^ b) & mask will simply return a's original value.

If the mask has a 1 at that bit position, (a ^ b) & mask will simply return the value of a ^ b. Now if we xor the value with a, we get a ^ (a ^ b) = (a ^ a) ^ b = b. This is a consequence of a ^ a = 0 -- any value xor'ed with itself will return zero. And then, as previously mentioned, zero xor'ed with any value will just give you the original value.

How to think in bits:

Read up on what others have done, and take note of their strategies. The Stanford site you link to is a pretty good resource -- there are often several techniques shown for a particular operation, allowing you to see the problem from different angles. You might have noticed that there are people who've submitted their own alternative solutions for a particular operation, which were inspired by the techniques applied to a different operation. You could take the same approach.

Also, it might help you to remember a handful of simple identities, which you can then string together for more useful operations. IMO listing out the results for each bit-combination is only useful for reverse-engineering someone else's work.

int3
+2  A: 

In order to create boolean expressions like that one, I think you'd have to learn some boolean algebra.

This looks good:

http://www.allaboutcircuits.com/vol_4/chpt_7/1.html

It even has a page on generating boolean expressions from truth tables.

It also has a section on Karnaugh Maps. To be honest, I've forgotten what those are, but they look like they could be useful for what you want to do.

http://www.allaboutcircuits.com/vol_4/chpt_8/1.html
dangph
congrats on actually answering the guys question :)
Pod
also, +1 for the Karnaugh maps. Great things.
Pod
maybe it's my browser, but the links come up empty
Abel
@Abel, yes, there was something weird about them. I've made them pre-formatted text now. They work if you copy-and-paste them.
dangph
Alternatively, use the Add Link button in the edit window, or use `<a href...>` notation, which should work. The underscore in the link `_` is otherwise interpreted as a start for *italics* which may've been the reason it turning wrong.
Abel
@Abel, I tried the Add Link button, but that didn't work. I did however use the original URL as the text of the link (if you follow me), and maybe that overrode the real link.
dangph
A: 

First learn the logical (that is, 1-bit) operators well. Try to write down some rules, like

a && b = b && a
1 && a = a
1 || a = 1
0 && a = ... //you get the idea. Come up with as many as you can.

Include the "logical" xor operator:

1 ^^ b = !b
0 ^^ b = ...

Once you have a feel for these, move onto bitwise operators. Try some problems, look at some common tricks and techniques. With a bit of practice you'll feel much more confident.

Artelius
+5  A: 

In this case, I can see that it works, but I am not sure what the logic is? And I am not sure I can create my own bit operations like this from scratch. How do I start thinking in bits?

People have answered your first question -- explaining the logic. I shall hopefully show you a terribly basic, long-winded but standard method of making any bit twiddling operations. (note, once you get used to working with bits you'll start thinking in & and | straight off without doing such nonsense).

  1. Figure out what you'd like your operation to do.
  2. Write out a FULL truth table.
  3. Either read the sum-of-products direct from the table or make a Karnaugh map. The km will reduce the final eqution a lot.
  4. ???
  5. Profit

Deriving for the example you gave. ie, where a mask selects bits from A or B. (0 is A, 1 is B)

This table is for 1 bit per input. I'm not doing more than one bit, as I don't want to waste my time :) ( why? 2^(2bits * 3inputs) = 64 cases :( 2^(3bits * 3inputs) = 512 cases :(()

But the good news is that in this case the operation is independant of the number of bits, so a 1 bit example is 100% fine. Infact it's recommended by me :)

| A | B | M || R |
============++====
| 0 | 0 | 0 || 0 |
| 0 | 0 | 1 || 0 |
| 0 | 1 | 0 || 0 |
| 0 | 1 | 1 || 1 |
| 1 | 0 | 0 || 1 |
| 1 | 0 | 1 || 0 |
| 1 | 1 | 0 || 1 |
| 1 | 1 | 1 || 1 |

Hopefully you can see how this truth table works.

how to get an expression from this? Two methods: KMaps and by-hand. Let's do it by-hand first, should we? :)

Looking at the points where R is true, we see:

| A | B | M || R |
============++====
| 0 | 1 | 1 || 1 |
| 1 | 0 | 0 || 1 |
| 1 | 1 | 0 || 1 |
| 1 | 1 | 1 || 1 |

From this we can dervive an expresion:

R = (~A &  B &  M) |
    ( A & ~B & ~M) |
    ( A &  B & ~M) |
    ( A &  B &  M) |

Hopefully you can see how this works: just or together the full expressions seen in each case. By full I imply that you need to not-variables i nthere.

Let's try it in python:

a = 0xAE #10101110b
b = 0x64 #01100011b
m = 0xF0 #11110000b
r = (~a & b & m) | ( a & ~b & ~m) | ( a &  b & ~m) | ( a &  b &  m)
print hex(r)

OUTPUT:

0x6E

These numbers are from Abel's example. The output is 0x6E, which is 01101110b. So it worked! Hurrah. (ps, it's possible to derive an expression for ~r from the first table, should you wish to do so. Just take the cases where r is 0).

This expression you've made is a boolean "sum of products", aka Disjunctive Normal Form, although DNF is really the term used when using first-order predicate logic. This expression is also pretty unweidly. Making it smaller is a tedious thing to do on paper, and is the kind of thing you'll do 500,000 times at Uni' on a CS degree if you take the compiler or hardware courses. (Highly recommended :))

So let's do some boolean algebra magic on this (don't try and follow this, it's a waste of time):

(~a & b & m) | ( a & ~b & ~m) | ( a &  b & ~m) | ( a &  b &  m)
|= ((~a & b & m) | ( a & ~b & ~m)) | ( a &  b & ~m) | ( a &  b &  m)

take that first sub-clause that I made:

((~a & b & m) | ( a & ~b & ~m))

|= (~a | (a & ~b & ~m)) & (b | ( a & ~b & ~m)) & (m | ( a & ~b & ~m))
|= ((~a | a) & (a | ~b) &( a | ~m)) & (b | ( a & ~b & ~m)) & (m | ( a & ~b & ~m))
|= (T & (a | ~b) &( a | ~m)) & (b | ( a & ~b & ~m)) & (m | ( a & ~b & ~m))
|= ((a | ~b) & (a | ~m)) & (b | ( a & ~b & ~m)) & (m | ( a & ~b & ~m))

etc etc etc. This is the massively tedious bit incase you didn't guess. So just whack the expression in a website of your choice, which will tell you

r = (a & ~m) | (b & m)

Hurrah! Correct result. Note, it might even go so far as giving you an expression involving XORs, but who cares? Actually, some people do, as the expression with ands and ors is 4 operations (1 or, 2 and, 1 neg), whilst r = a ^ ((a ^ b) & mask) is 3 (2 xor, 1 and).

Now, how do you do it with kmaps? Well, first you need to know how to make them, I'll leave you to do that. :) Just google for it. There's software available, but I think it's best to do it by hand -- it's more fun and the programs don't allow you to cheat. Cheat? Well, if you have lots of inputs, it's often best to reduce the table like so:

| A | B | M || R |
============++====
| X | X | 0 || A |
| X | X | 1 || B |

eg that 64 case table?

| A1| A0| B1| B0| M1| M0|| R1| R0|
========================++========
| X | X | X | X | 0 | 0 || A1| A0|
| X | X | X | X | 0 | 1 || A1| B0|
| X | X | X | X | 1 | 0 || B1| A0|
| X | X | X | X | 1 | 1 || B1| B0|

Boils down to 4 cases in this example :)

(Where X is "don't care".) Then put that table in your Kmap. Once again, an exercise for you to work out [ie, I've forgotten how to do this].

Hopefully you can now derive your own boolean madness, given a set of inputs and an expected set of outputs.

Have fun.

Pod
+1 Interesting and thorough, nice fast-paced tutorial
Abel
+1 for K-maps. Once you get used to them, they're super-handy for smaller binary truth tables.
Tim
A: 

How do I start thinking in bits?

Hank Warren's book Hacker's Delight will teach you to start thinking in bits.

Norman Ramsey
A: 

Maybe you dont need to think in bits - perhaps you can get your compiler to think in bits for you and you can focus on the actual problem you're trying to solve instead. Using bit manipulation directly in your code can produce some profoundly impenetrable (if impressive) code -- here's some nice macros (from the windows ddk) that demonstrate this

// from ntifs.h

// These macros are used to test, set and clear flags respectivly

#define FlagOn(_F,_SF) ((_F) & (_SF))

#define BooleanFlagOn(F,SF) ((BOOLEAN)(((F) & (SF)) != 0))

#define SetFlag(_F,_SF) ((_F) |= (_SF))

#define ClearFlag(_F,_SF) ((_F) &= ~(_SF))

now if you want to set a flag in a value you can simply say SetFlag(x, y) much clearer I think. Moreover if you focus on the problem you're trying to address with your bit fiddling the mechanics will become second nature without you having to expend any effort. Look after the bits and the bytes will look after themselves!

jolyon