Fastest known way to extract a single bit from a byte

bluto32

New Member
Joined
Jan 5, 2011
Messages
37
I have experimented with various ways of extracting a bit (at a specified position) from a byte, and have come up with a slightly convoluted way of doing this as follows:

VBA Code:
Sub Test()
    Dim PowerOf2(0 To 7) As Byte 'Store powers of 2 for efficiency
    Dim b As Byte
    Dim ExtractedBit(0 To 7) As Byte 'Really a bit array: only takes values 0 or 1
    Dim k As Long 'Bit counter
    
    'This loop allows powers of 2 to be calculated only once at the start.
    'Referring to PowerOf2(5) thousands of times in a big loop is faster than calculating 2^5 each time.
    For k = 0 To 7
        PowerOf2(k) = 2 ^ k
    Next k
    
    b = 173
    
    For k = 0 To 7
        'Extract kth bit, where bits are numbered 0 to 7 (right to left)
        ExtractedBit(k) = -CBool(b And PowerOf2(k))
        Debug.Print ExtractedBit(k)
    Next k
End Sub


The b And PowerOf2(k) returns 0 if the k bit is not set, and 2^k if it is set.
The CBool then coverts this to False (value 0) or True (value -1) respectively.
Finally, the leading minus sign converts the Boolean into the required value of 0 (unset) or 1 (set).

In massive loops, -CBool(b And PowerOf2(k)) has proved to be faster than using anything else I have tried. (For example, testing If b And PowerOf2(k) > 0 is considerably slower.) But it looks a bit weird (no pun intended...), and I can't help but think there's probably a faster and more natural way. Is there?

Also, can a variable be defined as a bit rather than a byte? I don't want to use Boolean, since I shall be using the bits in numerical calculations.
 
Glad to help.
What programming language I would recommend? That would be difficult to advise. In your Spectrum ZX era I managed to wreck the boot sector and partition table of my first 10 Mb harddrive. Since we didn't have the internet back then I was on my own and figured learning some basics of the Intel assembly language could bring back my harddisk. That was tougher than I could imagine, nevertheless, after a few months, a lot of blood and tears and many, many boot-ups from floppy I managed to get it work again. My brain back then was way better than at present ... LOL
I never felt the need to learn a higher programming language, it was all just for fun. Years later, Excel and VBA came my way by accident. Since the 64-bit era I don't do much with assembler anymore. Where under Windows XP (32 bit) you could run your 16-bit executables and could encapsulate some assembler instructions in a VBA procedure fairly simple, this is no longer possible due to the changed invocation protocol. Therefore nowadays you have to write your own DLL if you feel the need for speed.
Anyway, coming back to your question, I think I'd go for C#. Of the programming languages out there, C is closest to the machine. In addition, most compilers, at least from what I read, produce smooth running machine code.
Sometimes it's nice to take on a new challenge. I wish you good luck!
 
Upvote 0

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
A possibility? I have no experience with it but several years ago there was a compiler called PowerBASIC which was reported to be super, super fast HOWEVER, the compiler has not been updated for several years now as the inventor/programmer of PowerBASIC died at a young age but I see on its website that it runs on Windows 10 though. This version of basic has some similar commands to VB but it also has many, many more commands as well. I contains a RegExp engine, allows for assembler code to be bound directly into the basic code, supports objects, creates EXEs and DLLs. I don't think it has a native graphic designer (I see an add in available to do this)... you create everything in code. Anyway, if this interests you in any way, here is a link to its Reference Manual...

https://downloads.powerbasic.com/pdf/PowerBASIC Compiler for Windows v10.0.pdf
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,821
Members
449,049
Latest member
cybersurfer5000

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top