Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>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?</p> </blockquote> <p>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 &amp; and | straight off without doing such nonsense).</p> <ol> <li>Figure out what you'd like your operation to do.</li> <li>Write out a <strong>FULL</strong> truth table. </li> <li>Either read the sum-of-products direct from the table or make a Karnaugh map. The km will reduce the final eqution a <em>lot</em>.</li> <li>???</li> <li>Profit</li> </ol> <p>Deriving for the example you gave. ie, where a mask selects bits from A or B. (0 is A, 1 is B)</p> <p>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 :(()</p> <p>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 :)</p> <pre><code>| 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 | </code></pre> <p>Hopefully you can see how this truth table works.</p> <p>how to get an expression from this? Two methods: KMaps and by-hand. Let's do it by-hand first, should we? :)</p> <p>Looking at the points where R is true, we see:</p> <pre><code>| A | B | M || R | ============++==== | 0 | 1 | 1 || 1 | | 1 | 0 | 0 || 1 | | 1 | 1 | 0 || 1 | | 1 | 1 | 1 || 1 | </code></pre> <p>From this we can dervive an expresion:</p> <pre><code>R = (~A &amp; B &amp; M) | ( A &amp; ~B &amp; ~M) | ( A &amp; B &amp; ~M) | ( A &amp; B &amp; M) | </code></pre> <p>Hopefully you can see how this works: just or together the <em>full</em> expressions seen in each case. By full I imply that you need to not-variables i nthere.</p> <p>Let's try it in python:</p> <pre><code>a = 0xAE #10101110b b = 0x64 #01100011b m = 0xF0 #11110000b r = (~a &amp; b &amp; m) | ( a &amp; ~b &amp; ~m) | ( a &amp; b &amp; ~m) | ( a &amp; b &amp; m) print hex(r) </code></pre> <p>OUTPUT:</p> <pre><code>0x6E </code></pre> <p>These numbers are from Abel's example. The output is <code>0x6E</code>, which is <code>01101110b</code>. 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).</p> <p>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 :))</p> <p>So let's do some boolean algebra magic on this (don't try and follow this, it's a waste of time):</p> <pre><code>(~a &amp; b &amp; m) | ( a &amp; ~b &amp; ~m) | ( a &amp; b &amp; ~m) | ( a &amp; b &amp; m) |= ((~a &amp; b &amp; m) | ( a &amp; ~b &amp; ~m)) | ( a &amp; b &amp; ~m) | ( a &amp; b &amp; m) </code></pre> <p>take that <em>first</em> sub-clause that I made:</p> <pre><code>((~a &amp; b &amp; m) | ( a &amp; ~b &amp; ~m)) |= (~a | (a &amp; ~b &amp; ~m)) &amp; (b | ( a &amp; ~b &amp; ~m)) &amp; (m | ( a &amp; ~b &amp; ~m)) |= ((~a | a) &amp; (a | ~b) &amp;( a | ~m)) &amp; (b | ( a &amp; ~b &amp; ~m)) &amp; (m | ( a &amp; ~b &amp; ~m)) |= (T &amp; (a | ~b) &amp;( a | ~m)) &amp; (b | ( a &amp; ~b &amp; ~m)) &amp; (m | ( a &amp; ~b &amp; ~m)) |= ((a | ~b) &amp; (a | ~m)) &amp; (b | ( a &amp; ~b &amp; ~m)) &amp; (m | ( a &amp; ~b &amp; ~m)) </code></pre> <p>etc etc etc. This is the massively tedious bit incase you didn't guess. So just whack the expression in a website of your <a href="http://hopper.unco.edu/KARNAUGH/Function.html" rel="nofollow noreferrer">choice</a>, which will tell you</p> <pre><code>r = (a &amp; ~m) | (b &amp; m) </code></pre> <p>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 <code>and</code>s and <code>or</code>s is 4 operations (1 <code>or</code>, 2 <code>and</code>, 1 <code>neg</code>), whilst <code>r = a ^ ((a ^ b) &amp; mask)</code> is 3 (2 <code>xor</code>, 1 <code>and</code>). </p> <p>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:</p> <pre><code>| A | B | M || R | ============++==== | X | X | 0 || A | | X | X | 1 || B | </code></pre> <p>eg that 64 case table?</p> <pre><code>| 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| </code></pre> <p>Boils down to 4 cases in this example :)</p> <p>(Where X is "don't care".) Then put <em>that</em> table in your Kmap. Once again, an exercise for you to work out [ie, I've forgotten how to do this].</p> <p>Hopefully you can now derive your own boolean madness, given a set of inputs and an expected set of outputs.</p> <p>Have fun.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload