title: Bit Test (BT) in assembly
body: <p>I'm using bt to test if certain bits are set or unset in assembly. My code looks like this:</p>
  
  <pre><code>#define bv  %eax   //just renaming reg to make reading easier
  #define bit %edx   //rename reg
  /*
   * bitIsSetBV(unsigned int *bv, unsigned int bit)
   * this function returns 0 if bit at location bit is not set
   * it returns 1 if it is set
   */
  
  .text
  .global bitIsSetBV
  
  bitIsSetBV:
  movl 4(%esp), bv   //make room on stack for bv
  movl 8(%esp), bit  //make room on stack for bit
  
  bt bit, (bv)       //if bv[bit] is 0, set CF to 0, else, set CF to 1
  jc bitSet          // if CF is 1, jump to bitSet
  jnc bitNotSet      // if CF is 0, jump to bitNotSet
  
  bitNotSet:
  movl $0, bv        //return 0, bit is not set
  jmp Done           // jump to Done
  
  bitSet:
  movl $1, bv         // return 1, bit is set
  jmp Done           // jump to Done
  
  Done:
  ret                //end of code
  </code></pre>
  
  <p>Tester:</p>
  
  <blockquote>
  <pre><code>  assert(bitIsSetBV(bv, 0) == 1); // 0001
    assert(bitIsSetBV(bv, 1) == 0);
    assert(bitIsSetBV(bv, 2) == 0);
    assert(bitIsSetBV(bv, 3) == 0);
  
    assert(bitIsSetBV(bv, 4) == 0); // 1110
    assert(bitIsSetBV(bv, 5) == 1);
    assert(bitIsSetBV(bv, 6) == 1);
    assert(bitIsSetBV(bv, 7) == 1);
  </code></pre>
  </blockquote>
  
  <p>My code works for bit =0-5, but at bit=6, it does not. Why is that? I'm sure it's something I'm misunderstanding about bit level operations.</p>