Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Borland Delphi > Fastest way to ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 20 Topic 3713 of 3952
Post > Topic >>

Fastest way to get rid of accessive bits (chop off bits)

by "Skybuck Flying" <BloodyShame@[EMAIL PROTECTED] > Apr 27, 2008 at 01:04 PM

Hello,

So far this is (I think) my fastest way to chop of accessive bits from a 
byte (in Delphi).

// Mission:
// Value contains a binary value/code.
// BitCount specifies which bits of the value starting from bit position 
0/lsb should remain.
// so BitCount 5 means: Bits 0 to 4 must remain. Bits 5 to 7 must be 
cleared.
// The accessive bits beyond bit count are to be zero-ed/cleared.

var
    Bitcount : byte;
    Value : byte;
    Mask : byte;
begin
    Value := 255; // could be anything
    BitCount := 5; // could be zero to 8

    // 5 instructions, no memory usage, best one so far.
    Mask := not (255 shl BitCount);
    Value := Value and Mask;

    writeln( Value ); // displays 31.
    readln;
end.

// generated assembler for the method:

Project1.dpr.18: Mask := not (255 shl BitCount);
00409151 8BC8             mov ecx,eax
00409153 B0FF             mov al,$ff
00409155 D2E0             shl al,cl
00409157 F6D0             not al
Project1.dpr.19: Value := Value and Mask;
00409159 22D8             and bl,al

I am pretty sure there is a faster way in assembler.

I am mostly looking for high level code Delphi code.

But it could still be interesting to see any faster low level assembler
code 
in case I want to write an assembler routine in the future.

Who can optimize it ? :):):)

I tried different high level techniques, I already tried one high level 
technique using a larger mask and ****fting the low bits into the high bits

and then use the high byte to immediatly mask.. reducing the not 
instruction... but delphi compiler generated/used memory access which is 
probably worse.

But when coding directly in assembler it's probably possible to prevent
the 
memory access ;)

I haven't tried yet in assembler because I leave that fun to you guys the 
assembler-fan-boys :)

Can you write something better than the delphi compiler ? ;)

Bye,
  Skybuck.
 




 20 Posts in Topic:
Fastest way to get rid of accessive bits (chop off bits)
"Skybuck Flying"  2008-04-27 13:04:47 
Re: Fastest way to get rid of accessive bits (chop off bits)
James Harris <james.ha  2008-04-27 11:43:22 
Re: Fastest way to get rid of accessive bits (chop off bits)
"Skybuck Flying"  2008-04-27 22:19:03 
Re: Fastest way to get rid of accessive bits (chop off bits)
Ivan Levashew <octagra  2008-04-28 03:04:16 
Re: Fastest way to get rid of accessive bits (chop off bits)
"Skybuck Flying"  2008-04-27 22:10:55 
Re: Fastest way to get rid of accessive bits (chop off bits)
James Harris <james.ha  2008-04-27 13:18:54 
Re: Fastest way to get rid of accessive bits (chop off bits)
"Skybuck Flying"  2008-04-27 22:37:45 
Re: Fastest way to get rid of accessive bits (chop off bits)
Ivan Levashew <octagra  2008-04-28 04:55:07 
Re: Fastest way to get rid of accessive bits (chop off bits)
"Skybuck Flying"  2008-04-28 09:03:17 
Re: Fastest way to get rid of accessive bits (chop off bits)
Ivan Levashew <octagra  2008-04-28 21:12:14 
Re: Fastest way to get rid of accessive bits (chop off bits)
"Skybuck Flying"  2008-04-27 22:31:01 
Re: Fastest way to get rid of accessive bits (chop off bits)
"Skybuck Flying"  2008-04-27 22:34:43 
Re: Fastest way to get rid of accessive bits (chop off bits)
James Harris <james.ha  2008-04-27 13:40:34 
Re: Fastest way to get rid of accessive bits (chop off bits)
"Skybuck Flying"  2008-04-27 23:03:58 
Re: Fastest way to get rid of accessive bits (chop off bits)
"Skybuck Flying"  2008-04-28 13:55:36 
Re: Fastest way to get rid of accessive bits (chop off bits)
"Skybuck Flying"  2008-04-29 13:58:49 
Re: Fastest way to get rid of accessive bits (chop off bits)
"Skybuck Flying"  2008-04-29 14:09:04 
Re: Fastest way to get rid of accessive bits (chop off bits)
"Skybuck Flying"  2008-04-29 15:05:38 
Re: Fastest way to get rid of accessive bits (chop off bits)
"Skybuck Flying"  2008-05-05 17:08:26 
Re: Fastest way to get rid of accessive bits (chop off bits)
"Skybuck Flying"  2008-05-05 17:44:55 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon Oct 13 4:37:34 CDT 2008.