public void Bits_BitCountTest() { Assert.AreEqual(0, Bits.BitCount(byte.MinValue)); Assert.AreEqual(Bits.kByteBitCount / 2, Bits.BitCount(unchecked ((byte)kEvenBits))); Assert.AreEqual(Bits.kByteBitCount / 2, Bits.BitCount(unchecked ((byte)kOddBits))); Assert.AreEqual(Bits.kByteBitCount / 2, Bits.BitCount(unchecked ((byte)kEvenNybbles))); Assert.AreEqual(Bits.kByteBitCount / 2, Bits.BitCount(unchecked ((byte)kOddNybbles))); Assert.AreEqual(Bits.kByteBitCount, Bits.BitCount(byte.MaxValue)); Assert.AreEqual(0, Bits.BitCount(ushort.MinValue)); Assert.AreEqual(Bits.kInt16BitCount / 2, Bits.BitCount(unchecked ((ushort)kEvenBits))); Assert.AreEqual(Bits.kInt16BitCount / 2, Bits.BitCount(unchecked ((ushort)kOddBits))); Assert.AreEqual(Bits.kInt16BitCount / 2, Bits.BitCount(unchecked ((ushort)kEvenNybbles))); Assert.AreEqual(Bits.kInt16BitCount / 2, Bits.BitCount(unchecked ((ushort)kOddNybbles))); Assert.AreEqual(Bits.kInt16BitCount, Bits.BitCount(ushort.MaxValue)); Assert.AreEqual(0, Bits.BitCount(uint.MinValue)); Assert.AreEqual(Bits.kInt32BitCount / 2, Bits.BitCount(unchecked ((uint)kEvenBits))); Assert.AreEqual(Bits.kInt32BitCount / 2, Bits.BitCount(unchecked ((uint)kOddBits))); Assert.AreEqual(Bits.kInt32BitCount / 2, Bits.BitCount(unchecked ((uint)kEvenNybbles))); Assert.AreEqual(Bits.kInt32BitCount / 2, Bits.BitCount(unchecked ((uint)kOddNybbles))); Assert.AreEqual(Bits.kInt32BitCount, Bits.BitCount(uint.MaxValue)); Assert.AreEqual(0, Bits.BitCount(ulong.MinValue)); Assert.AreEqual(Bits.kInt64BitCount / 2, Bits.BitCount(unchecked ((ulong)kEvenBits))); Assert.AreEqual(Bits.kInt64BitCount / 2, Bits.BitCount(unchecked ((ulong)kOddBits))); Assert.AreEqual(Bits.kInt64BitCount / 2, Bits.BitCount(unchecked ((ulong)kEvenNybbles))); Assert.AreEqual(Bits.kInt64BitCount / 2, Bits.BitCount(unchecked ((ulong)kOddNybbles))); Assert.AreEqual(Bits.kInt64BitCount, Bits.BitCount(ulong.MaxValue)); }
static void TestBitCount() { Debug.Assert(Bits.BitCount(uint.MaxValue) == 32); Debug.Assert(Bits.BitCount(0) == 0); Debug.Assert(Bits.BitCount(1) == 1); Debug.Assert(Bits.BitCount(ushort.MaxValue) == 16); Debug.Assert(Bits.BitCount(ushort.MaxValue - 1) == 15); }
public void BitCount() { Assert.AreEqual(63, Bits.BitCount(~1ul)); Assert.AreEqual(0, Bits.BitCount(0)); Assert.AreEqual(2, Bits.BitCount(9)); Assert.AreEqual(2, Bits.BitCount(12)); Assert.AreEqual(3, Bits.BitCount(14)); Assert.AreEqual(7, Bits.BitCount(254)); }
private string UnsignedRepresentation(Constant u) { ulong m; var b = u.DataType.BitSize; if (b == 0x40) { m = ~0ul; } else { m = (1ul << b)-1; } ulong msb = (1ul << (b - 1)); ulong p = u.ToUInt64(); var decRep = p.ToString(CultureInfo.InvariantCulture); var hexRep = p.ToString("X", CultureInfo.InvariantCulture); var padHexRep = hexRep; if (hexRep.Length < decRep.Length) { padHexRep = new string('0', decRep.Length - hexRep.Length) + hexRep; } var decEntropy = Entropy(decRep, DecimalSymbols); var hexEntropy = Entropy(padHexRep, HexSymbols); if (decEntropy < hexEntropy) { return decRep; } else { var sb = new StringBuilder(); if ((p & msb) != 0 && Bits.BitCount(m & p) > Bits.BitCount(m & ~p)) { sb.Append('~'); p = m & ~p; hexRep = p.ToString("X", CultureInfo.InvariantCulture); } sb.Append("0x"); int length = hexRep.Length; int pad; if (length <= 2) pad = 2 - length; else if (length <= 4) pad = 4 - length; else if (length <= 8) pad = 8 - length; else pad = 0; sb.Append('0', pad); sb.Append(hexRep); return sb.ToString(); } }
public void TestBitCountByte() { Assert.AreEqual(0, Bits.BitCount((byte)0)); Assert.AreEqual(1, Bits.BitCount((byte)1)); Assert.AreEqual(2, Bits.BitCount(default(byte).SetBit(Bits.SizeOfByteInBits / 2).SetBit(Bits.SizeOfByteInBits - 1))); var allBitsSet = byte.MinValue == default(byte) ? byte.MaxValue : unchecked ((byte)-1); Assert.AreEqual(Bits.SizeOfByteInBits, Bits.BitCount(allBitsSet)); Assert.AreEqual(Bits.SizeOfByteInBits - 2, Bits.BitCount(allBitsSet.ClearBit(Bits.SizeOfByteInBits / 2).ClearBit(0))); // fuzz testing for (var i = 0; i < FuzzTestIterations; ++i) { var randomValue = this.GetRandomByte(); var binaryString = Bits.ToShortBinaryString(randomValue); Assert.AreEqual(binaryString.Count(ch => ch == '1'), Bits.BitCount(randomValue), binaryString); } }
/// <inheritdoc /> public override int GetMachineIdIndex(MachineId currentMachineId) { int dataIndex = Offset + currentMachineId.Index / 8; if (dataIndex >= Data.Length) { return(-1); } var dataBitPosition = 7 - (currentMachineId.Index % 8); int machineIdIndex = 0; byte redisChar; for (int i = Offset; i < dataIndex; i++) { redisChar = Data[i]; if (redisChar != 0) { machineIdIndex += Bits.BitCount(redisChar); } } redisChar = Data[dataIndex]; int position = 0; while (redisChar != 0) { if ((redisChar & MaxCharBitMask) != 0) { if (dataBitPosition == position) { return(machineIdIndex); } machineIdIndex++; } redisChar <<= 1; position++; } return(-1); }
public void Bits_BitCountTest2() { { const ulong kBitCountValue = 0xAAAAAAAAAAAAAAAA; int i32; int expected_bit_count; expected_bit_count = Bits.kByteBitCount / 2; i32 = Bits.BitCount(unchecked ((byte)kBitCountValue)); Assert.AreEqual(expected_bit_count, i32); expected_bit_count = Bits.kInt16BitCount / 2; i32 = Bits.BitCount(unchecked ((ushort)kBitCountValue)); Assert.AreEqual(expected_bit_count, i32); expected_bit_count = Bits.kInt32BitCount / 2; i32 = Bits.BitCount(unchecked ((uint)kBitCountValue)); Assert.AreEqual(expected_bit_count, i32); expected_bit_count = Bits.kInt64BitCount / 2; i32 = Bits.BitCount(kBitCountValue); Assert.AreEqual(expected_bit_count, i32); } { uint u32; ulong u64; u32 = Bits.BitCountToMask32(Bits.kInt32BitCount); Assert.AreEqual(u32, uint.MaxValue); u32 = Bits.BitCountToMask32(Bits.kInt32BitCount - 1); Assert.AreEqual(u32, uint.MaxValue >> 1); u64 = Bits.BitCountToMask64(Bits.kInt64BitCount); Assert.AreEqual(u64, ulong.MaxValue); u64 = Bits.BitCountToMask64(Bits.kInt64BitCount - 1); Assert.AreEqual(u64, ulong.MaxValue >> 1); } }
public static HandEvaluation EvaluateHand(ulong cards) { int numberOfCards = Bits.BitCount(cards); uint four_mask, three_mask, two_mask; HandEvaluation ret = new HandEvaluation(cards, 0, 0); #if DEBUG // This functions supports 1-7 cards if (numberOfCards < 1 || numberOfCards > 7) { throw new ArgumentOutOfRangeException("numberOfCards"); } #endif // Seperate out by suit uint sc = (uint)((cards >> (0)) & 0x1fffUL); uint sd = (uint)((cards >> (13)) & 0x1fffUL); uint sh = (uint)((cards >> (26)) & 0x1fffUL); uint ss = (uint)((cards >> (39)) & 0x1fffUL); uint ranks = sc | sd | sh | ss; uint n_ranks = Bits.nBitsTable[ranks]; uint n_dups = ((uint)(numberOfCards - n_ranks)); /* Check for straight, flush, or straight flush, and return if we can * determine immediately that this is the best possible hand */ if (n_ranks >= 5) { if (Bits.nBitsTable[ss] >= 5) { uint st = Bits.straightTable[ss]; if (st != 0) { return(new HandEvaluation(cards, HandTypes.StraightFlush, HANDTYPE_VALUE_STRAIGHTFLUSH + (st << TOP_CARD_SHIFT))); } else { ret = new HandEvaluation(cards, HandTypes.Flush, HANDTYPE_VALUE_FLUSH + Bits.topFiveCardsTable[ss]); } } else if (Bits.nBitsTable[sh] >= 5) { uint st = Bits.straightTable[sh]; if (st != 0) { return(new HandEvaluation(cards, HandTypes.StraightFlush, HANDTYPE_VALUE_STRAIGHTFLUSH + (st << TOP_CARD_SHIFT))); } else { ret = new HandEvaluation(cards, HandTypes.Flush, HANDTYPE_VALUE_FLUSH + Bits.topFiveCardsTable[sh]); } } else if (Bits.nBitsTable[sd] >= 5) { uint st = Bits.straightTable[sd]; if (st != 0) { return(new HandEvaluation(cards, HandTypes.StraightFlush, HANDTYPE_VALUE_STRAIGHTFLUSH + (st << TOP_CARD_SHIFT))); } else { ret = new HandEvaluation(cards, HandTypes.Flush, HANDTYPE_VALUE_FLUSH + Bits.topFiveCardsTable[sd]); } } else if (Bits.nBitsTable[sc] >= 5) { uint st = Bits.straightTable[sc]; if (st != 0) { return(new HandEvaluation(cards, HandTypes.StraightFlush, HANDTYPE_VALUE_STRAIGHTFLUSH + (st << TOP_CARD_SHIFT))); } else { ret = new HandEvaluation(cards, HandTypes.Flush, HANDTYPE_VALUE_FLUSH + Bits.topFiveCardsTable[sc]); } } else { uint st = Bits.straightTable[ranks]; if (st != 0) { ret = new HandEvaluation(cards, HandTypes.Straight, HANDTYPE_VALUE_STRAIGHT + (st << TOP_CARD_SHIFT)); } }; /* * Another win -- since there can't be a FH/Quads with 5 ranks present, * which is true most of the time when there is a made hand, then if we've * found a five card hand, just return. This skips the whole process of * computing two_mask/three_mask/etc. */ if (ret.Value != 0) { return(ret); } } /* * By the time we're here, either: * 1) there's no five-card hand possible (flush or straight), or * 2) there's a flush or straight, but we know that there are enough * duplicates to make a full house / quads possible. */ switch (n_dups) { case 0: /* It's a no-pair hand */ return(new HandEvaluation(cards, HandTypes.HighCard, HANDTYPE_VALUE_HIGHCARD + Bits.topFiveCardsTable[ranks])); case 1: { /* It's a one-pair hand */ ret.Type = HandTypes.Pair; uint t, kickers; two_mask = ranks ^ (sc ^ sd ^ sh ^ ss); ret.Value = (uint)(HANDTYPE_VALUE_PAIR + (Bits.topCardTable[two_mask] << TOP_CARD_SHIFT)); t = ranks ^ two_mask; /* Only one bit set in two_mask */ /* Get the top five cards in what is left, drop all but the top three * cards, and shift them by one to get the three desired kickers */ kickers = (Bits.topFiveCardsTable[t] >> CARD_WIDTH) & ~FIFTH_CARD_MASK; ret.Value += kickers; return(ret); } case 2: /* Either two pair or trips */ two_mask = ranks ^ (sc ^ sd ^ sh ^ ss); if (two_mask != 0) { uint t = ranks ^ two_mask; /* Exactly two bits set in two_mask */ ret.Type = HandTypes.TwoPair; ret.Value = (uint)(HANDTYPE_VALUE_TWOPAIR + (Bits.topFiveCardsTable[two_mask] & (TOP_CARD_MASK | SECOND_CARD_MASK)) + (Bits.topCardTable[t] << THIRD_CARD_SHIFT)); return(ret); } else { ret.Type = HandTypes.Trips; uint t, second; three_mask = ((sc & sd) | (sh & ss)) & ((sc & sh) | (sd & ss)); ret.Value = (uint)(HANDTYPE_VALUE_TRIPS + (Bits.topCardTable[three_mask] << TOP_CARD_SHIFT)); t = ranks ^ three_mask; /* Only one bit set in three_mask */ second = Bits.topCardTable[t]; ret.Value += (second << SECOND_CARD_SHIFT); t ^= (1U << (int)second); ret.Value += (uint)(Bits.topCardTable[t] << THIRD_CARD_SHIFT); return(ret); } default: /* Possible quads, fullhouse, straight or flush, or two pair */ four_mask = sh & sd & sc & ss; if (four_mask != 0) { ret.Type = HandTypes.FourOfAKind; uint tc = Bits.topCardTable[four_mask]; ret.Value = (uint)(HANDTYPE_VALUE_FOUR_OF_A_KIND + (tc << TOP_CARD_SHIFT) + ((Bits.topCardTable[ranks ^ (1U << (int)tc)]) << SECOND_CARD_SHIFT)); return(ret); } ; /* Technically, three_mask as defined below is really the set of * bits which are set in three or four of the suits, but since * we've already eliminated quads, this is OK */ /* Similarly, two_mask is really two_or_four_mask, but since we've * already eliminated quads, we can use this shortcut */ two_mask = ranks ^ (sc ^ sd ^ sh ^ ss); if (Bits.nBitsTable[two_mask] != n_dups) { /* Must be some trips then, which really means there is a * full house since n_dups >= 3 */ ret.Type = HandTypes.FullHouse; uint tc, t; three_mask = ((sc & sd) | (sh & ss)) & ((sc & sh) | (sd & ss)); ret.Value = HANDTYPE_VALUE_FULLHOUSE; tc = Bits.topCardTable[three_mask]; ret.Value += (tc << TOP_CARD_SHIFT); t = (two_mask | three_mask) ^ (1U << (int)tc); ret.Value += (uint)(Bits.topCardTable[t] << SECOND_CARD_SHIFT); return(ret); } ; if (ret.Value != 0) /* flush and straight */ { return(ret); } else { /* Must be two pair */ ret.Type = HandTypes.TwoPair; uint top, second; ret.Value = HANDTYPE_VALUE_TWOPAIR; top = Bits.topCardTable[two_mask]; ret.Value += (top << TOP_CARD_SHIFT); second = Bits.topCardTable[two_mask ^ (1 << (int)top)]; ret.Value += (second << SECOND_CARD_SHIFT); ret.Value += (uint)((Bits.topCardTable[ranks ^ (1U << (int)top) ^ (1 << (int)second)]) << THIRD_CARD_SHIFT); return(ret); } } }
public static ulong FindCards(HandEvaluation eval) { int numberOfCards = Bits.BitCount(eval.Cards); int remove = numberOfCards - 5; if (remove <= 0) { return(eval.Cards); } uint sc = (uint)((eval.Cards >> (0)) & 0x1fffUL); uint sd = (uint)((eval.Cards >> (13)) & 0x1fffUL); uint sh = (uint)((eval.Cards >> (26)) & 0x1fffUL); uint ss = (uint)((eval.Cards >> (39)) & 0x1fffUL); uint ranks = sc | sd | sh | ss; uint n_ranks = Bits.nBitsTable[ranks]; // Flush, Straight Flush, Straight if (n_ranks >= 5) { if (Bits.nBitsTable[ss] >= 5) { if (Bits.straightTable[ss] != 0) { return(0x1FUL << (Bits.straightTable[ss] - 4 + 39)); } else { return((ulong)Bits.LeftBits(ss, 5) << 39); } } if (Bits.nBitsTable[sh] >= 5) { if (Bits.straightTable[sh] != 0) { return(0x1FUL << (Bits.straightTable[sh] - 4 + 26)); } else { return((ulong)Bits.LeftBits(sh, 5) << 26); } } if (Bits.nBitsTable[sd] >= 5) { if (Bits.straightTable[sd] != 0) { return(0x1FUL << (Bits.straightTable[sd] - 4 + 13)); } else { return((ulong)Bits.LeftBits(sd, 5) << 13); } } if (Bits.nBitsTable[sc] >= 5) { if (Bits.straightTable[sc] != 0) { return(0x1FUL << (Bits.straightTable[sc] - 4 + 0)); } else { return((ulong)Bits.LeftBits(sc, 5) << 0); } } if (Bits.straightTable[ranks] != 0) { uint stBits = (uint)0x1F << (Bits.straightTable[ranks] - 4); ss &= stBits; stBits ^= ss; sh &= stBits; stBits ^= sh; sd &= stBits; stBits ^= sd; sc &= stBits; return(((ulong)sc << 0) | ((ulong)sd << 13) | ((ulong)sh << 26) | ((ulong)ss << 39)); } } uint n_dups = ((uint)(numberOfCards - n_ranks)); switch (n_dups) { case 0: // High Card uint hcBits = Bits.LeftBits(ranks, 5); ss &= hcBits; hcBits ^= ss; sh &= hcBits; hcBits ^= sh; sd &= hcBits; hcBits ^= sd; sc &= hcBits; return(((ulong)sc << 0) | ((ulong)sd << 13) | ((ulong)sh << 26) | ((ulong)ss << 39)); case 1: // One Pair uint keepBits = ranks ^ (sc ^ sd ^ sh ^ ss); ranks ^= keepBits; ranks = Bits.LeftBits(ranks, 3) | keepBits; ss &= ranks; ranks = (ranks ^ ss) | keepBits; sh &= ranks; ranks = (ranks ^ sh) | keepBits; sd &= ranks; ranks = (ranks ^ sd) | keepBits; sc &= ranks; return(((ulong)sc << 0) | ((ulong)sd << 13) | ((ulong)sh << 26) | ((ulong)ss << 39)); case 2: // Two Pair or Trips keepBits = ranks ^ (sc ^ sd ^ sh ^ ss); int otherCards = 1; if (keepBits == 0) { // Or Trips keepBits = ((sc & sd) | (sh & ss)) & ((sc & sh) | (sd & ss)); otherCards = 2; } ranks ^= keepBits; ranks = Bits.LeftBits(ranks, otherCards) | keepBits; ss &= ranks; ranks = (ranks ^ ss) | keepBits; sh &= ranks; ranks = (ranks ^ sh) | keepBits; sd &= ranks; ranks = (ranks ^ sd) | keepBits; sc &= ranks; return(((ulong)sc << 0) | ((ulong)sd << 13) | ((ulong)sh << 26) | ((ulong)ss << 39)); default: /* Possible quads, fullhouse, or three pair */ keepBits = sc & sd & sh & ss; otherCards = 1; if (keepBits == 0) { keepBits = ranks ^ (sc ^ sd ^ sh ^ ss); if (Bits.nBitsTable[keepBits] == n_dups) { // three pair, so keep the best 2 pair keepBits = Bits.LeftBits(keepBits, 2); } else { // Must be trips somewhere, so fullhouse keepBits |= ((sc & sd) | (sh & ss)) & ((sc & sh) | (sd & ss)); otherCards = 0; } } ranks ^= keepBits; ranks = Bits.LeftBits(ranks, otherCards) | keepBits; ss &= ranks; ranks = (ranks ^ ss) | keepBits; sh &= ranks; ranks = (ranks ^ sh) | keepBits; sd &= ranks; ranks = (ranks ^ sd) | keepBits; sc &= ranks; return(((ulong)sc << 0) | ((ulong)sd << 13) | ((ulong)sh << 26) | ((ulong)ss << 39)); } }
public static BitVectorUserInterfaceData ForFlagsEnum(Type enumType, int explicitNumberOfBits = TypeExtensions.kNone) { Contract.Requires <ArgumentNullException>(enumType != null); Contract.Requires(Reflection.Util.IsEnumType(enumType)); Contract.Requires(explicitNumberOfBits.IsNoneOrPositive()); Contract.Ensures(Contract.Result <BitVectorUserInterfaceData>() != null); var bit_field_infos = Reflection.Util.GetEnumFields(enumType); var bit_ui_infos = new List <BitUserInterfaceData>(Bits.kInt64BitCount); bool find_highest_index = explicitNumberOfBits.IsNone(); int highest_index = explicitNumberOfBits - 1; foreach (var bit_field_info in bit_field_infos) { ulong flag = Convert.ToUInt64(bit_field_info.GetRawConstantValue(), Util.InvariantCultureInfo); if (Bits.BitCount(flag) > 0) { continue; } int bit_index = Bits.IndexOfHighestBitSet(flag); if (find_highest_index) { highest_index = System.Math.Max(highest_index, bit_index); if (bit_field_info.Name == EnumBitEncoderBase.kFlagsMaxMemberName) { highest_index--; find_highest_index = false; } } if (!find_highest_index && bit_index > highest_index) { continue; } bit_ui_infos.EnsureCount(bit_index + 1); if (bit_ui_infos[bit_index] != null) { continue; } var bit_ui_info = new BitUserInterfaceData(); SetBitInfoFromFieldInfo(bit_ui_info, bit_field_info); if (bit_ui_info.CanNotBeRendered) { continue; } bit_ui_infos[bit_index] = bit_ui_info; } var info = new BitVectorUserInterfaceData(); info.SetInfoFromFactoryData(bit_ui_infos); return(info); }
public virtual void Reset(ulong dealtCards = 0x0UL) { DealtCards = dealtCards; RemainingInDeck = CardCount - Bits.BitCount(dealtCards); }
public virtual ulong CompleteCards(int numberOfCards, ulong hand, Random rand = null) { return(DealCards(numberOfCards - Bits.BitCount(hand), rand) | hand); }
public virtual HandEvaluation PokerEvaluate(ulong cards) { return(PokerEvaluate(cards, Bits.BitCount(cards))); }