From 35e0024a8dcd074de8879545847721c983a087b2 Mon Sep 17 00:00:00 2001 From: Eric Anderson Date: Thu, 3 Jul 2014 13:58:12 -0700 Subject: Add comments to bitwise matching --- capset.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/capset.go b/capset.go index 3fcc814..ae77571 100644 --- a/capset.go +++ b/capset.go @@ -144,11 +144,16 @@ func (d *Deck) hasSetWithLast(tableau []Card) bool { } i := len(tableau) - 1 for j := 0; j < i; j++ { + // Both same and diff are trying to get 11 for each attribute to indicate a + // match. same := ^(tableau[i].attrs ^ tableau[j].attrs) diff := tableau[i].attrs ^ tableau[j].attrs for k := j + 1; k < i; k++ { + // same and diff become fully computed. 11 indicates match. sameFinal := same & ^(tableau[i].attrs ^ tableau[k].attrs) diffFinal := diff ^ tableau[k].attrs + // Shift and AND so that we have a single bit to indicate success. This is + // necessary for the OR to function appropriately. sameSingle := sameFinal & (sameFinal >> 1) diffSingle := diffFinal & (diffFinal >> 1) if (sameSingle|diffSingle)&d.isSetBitMask == d.isSetBitMask { -- cgit v1.2.3-70-g09d2