help with bits and bytes

Posted on 2005-05-11
Last Modified: 2010-04-05
hi, I starting learning delphi 2 months ago and it was a great joy. I created a program to evaluate poker hand in holdem games at online poker rooms with it.
I was stuck at last trying to find a way to implement a rating system that gives a numeric rank to any given 5 cards. I used 64bit integer values to assign ranks.
for example 231221211201191 would translate to AceC KingC QueenC JackC TenC
Ace has value of 23 and king 22. The 231 stands for Ace+Club(which is represented by the 1).
Then I multiply the resultnig rank with 10000000000000000 and add 9 if flush and 8 if straight etc...
This way I am both encoding the cards and rating the hands they constitute.
The program works fine. But I found a poker library here
using a different kind of rating by manipulating bits! this puzzled me as I have never dealt with these.

The comment in the library read:

   TcaaHandRating - this type is the core type of the rating system. It is
    implemented as a 32 bit Integer. Information about the hand is mapped as
    described below:

       rank           card5  card4  card3  card2  card1
 332222222222   1111   1111   1100   0000   0000
 109876543210   9876   5432   1098   7654   3210

    The most significant 12 bits (20..31) the contain hand rank.

    The lowest 20 bits (0..19) are divided into five nibbles which contain
    the ordinal values of the cards. The order the cards are in are based on
    the rules for evaluating the hands of poker and are described below:

Now how can a card be encoded in a nibble which is four bits as I read somewhere? Every card in my case like 231 occupies a byte and does not fit into a nibble which explains why i had to use 64bits
Any help appreciated.
And I am ready to explain my question more if needed.

Question by:oalawna

    Author Comment

    There is another question about the binary representation of number but let us close this Q first.
    LVL 1

    Accepted Solution


    As you noted they must be using a different rating scheme than you are. The four bits gives them up to 16 possible values which is enough to store the card value, but not necessarily the suit.

    Maybe the rank contains the suits, etc.

    Author Comment

    i see, but there are 52 cards. then this means that they are not encoding suits also

    Author Comment

    one more question. Is there is anything i should care about using int64. I mean drawbacks or expected problems?
    LVL 1

    Assisted Solution


    No there shouldn't be. That is still a pretty small chunk of memory in the scheme of things. It really won't increase your total memory needs by too much.

    Delphi Help says: "Note:Most standard routines that take integer arguments truncate Int64 values to 32 bits. However, the High, Low, Succ, Pred, Inc, Dec, IntToStr, and IntToHex routines fully support Int64 arguments. Also, the Round, Trunc, StrToInt64, and StrToInt64Def functions return Int64 values. A few routines—including Ord—cannot take Int64 values at all."

    So just scout out the routines you want to use and make sure they handle Int64. Unless you want to build your own :)

    You could also use a variant record. That way you could access the Int64 as two int32 or as an Int64 when needed. (The variant record shares memory space). For example:

      TCard = record
        case byte of
        1: (A : Integer;
            B : Integer);
        2: (C : Int64;)

      TForm1 = class(TForm)
        BitBtn1: TBitBtn;
        procedure BitBtn1Click(Sender: TObject);
        { Private declarations }
        Card : TCard;
        { Public declarations }

      Form1: TForm1;


    {$R *.dfm}

    procedure TForm1.BitBtn1Click(Sender: TObject);
      Card.A:= 23;
      Card.B:= 44;

    Author Comment

    thanks Jturbin.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
    This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now