The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,862 other subscribers

For my link archive: Counting the leading zeroes and ones in a binary number with C#

Posted by jpluimers on 2025/03/13

From a while back, but still interesting:

  1. [Wayback/Archive] Counting the leading zeroes in a binary number with C#
  2. [Wayback/Archive] c# – Getting the number of leading 1 bits – Stack Overflow (thanks [Wayback/Archive] Barry Kelly and [Wayback/Archive] SoapBox)

Especially the first link explains the algorithm very well and is similar to links referred to from the Stack Overflow question as it is based on counting ones (and leading ones are basically leading zeros but bit-inverted).

It also explains a cool thing for leading zeros: modern CPU have instructions which .NET Core.

Via:

  1. [Wayback/Archive] Andrew Lock “Sock” on Twitter: “Blogged: Counting the leading zeroes in a binary number with C# #dotnet #dotnetcore #csharp”

  2. [Wayback/Archive] Jeroen Wiert Pluimers on Twitter: “@andrewlocknet Cool. This is kind of the opposite I asked a few years back a @StackOverflow”

References:

--jeroen

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.