Using Int64 as a bit container
Posted on 2001-08-13
I've recently written a DES (cryptography) in Delphi which uses a record to store each block (this record contains a BitCount field and an array of Bytes for the actual data). This record-method is really slow because I access these bits ALOT in sevral nested loops. I've also looked at the TBits class in VCL but its also too slow for me. This is why I want to use the Int64 data type as a bit container.
I intend to use it like this:
Container := 0;
//set bit number 8
Container := Container or BinPwr;
//read bit number 8
if Container AND BinPwr > 0 then
ShowMessage('This happens if the 8th bit is set');
This method is fast enough if I use a pre-calculated BinPwr array which contains the binary powers, eg; 1, 2, 4, 8, 16, 32 etc etc. Since I need to access up to 64 different bits I also need a BinPwr array with 64 elements.
My problem atm is; for nine days I've tried to implement this little (very simple) bit container system. But it does not work, the Int64 refuses to hold 64 different bits -- I only manage to save 63 bits in it.
Since the Int64 integer type is actually signed the last entry in the BinPwr should be equal to
-9223372036854775808 which is the largest negative integer that can be stored in an Int64.
My first problem was that I could not type
-9223372036854775808 in the Delphi IDE. It said that this number was "Integer constant to large". But I know my maths and this *should* work because ?2^63 =
-9223372036854775808 and strangely I can avoid this by writing -9223372036854775807-1 or Low(Int64) instead?! I suspect this is an IDE bug. If I then execute ShowMessage(IntToStr(i)); the message box clearly says -9223372036854775808 so apparently Delphi can handle this number but the IDE (the pre-processor or token-parser) rejects it.
And when I try to execute:
i := 0;
i := i or -9223372036854775807-1;
if i and -9223372036854775807-1 then
ShowMessage('This *SHOULD* happend!!');
The above message box does NOT appear!??!?! Now I'm getting really confused here. Inserting any other number (instead of -9223372036854775807-1) makes it work, but that's irrelevant.
Anyway, I will give the points to any one hero who will give me a working example of a bit container which can:
(1) show the bits in a string; eg a function which takes a Int64 and shows the 64 bits in 0s and 1s. something like bitsToStr()
(2) set and read 64 different bits
(3) is correctly compatible with the shl,shr operators. Meaning that if I set bit 8 then executes a shl on it, the "1" should move to the left when I re-execute the showmessage(bitsToStr(i)) function.
I've worked with network bit-order, motorola bitorder, intel bitorder and signed and unsigned and blah blah and this bit thing it really starting to get to me. For nine days I've had nothing but failures, this tends to wear a person out. I'm sick and tired of bits. Please help me.
If you are unsure of what I'm asking for, please post a comment.
I have more points if needed I REALLY REALLY want this one solved.