Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1096285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:15:41+00:00 2026-05-17T00:15:41+00:00

I need to compare two numbers and look for similarities in more significant bits.

  • 0

I need to compare two numbers and look for similarities in more significant bits. I’m trying to determine the number of least significant bits that differ.

10111000
10111011

184 and 187 require an offset of two, because only two least significant bits differ.

10111011
11111011

187 and 251 require an offset of seven, because the seventh least significant bit differs.

My first idea was to XOR the numbers together, then bit-shift right until the number equaled zero. I feel like there is a better bit-wise solution to this that doesn’t involve loops, but I haven’t done enough bit-twiddling of my own to come up with it.

The solution needs to work for any 64 bits, as my numbers are being stored as UInt64. This is being written in C#, but the solution is most likely a language agnostic one.


11101101
11010101

Would need an offset of 6 bits. I’m trying to find how many similar bits I can take off the top.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:15:42+00:00Added an answer on May 17, 2026 at 12:15 am
    #include <stdio.h>
    #include <stdlib.h>
    
    #define TO_L(s) (strtol((s), NULL, 16))
    
    int tsb(unsigned long xa, unsigned long xb) {
      unsigned long v = xa ^ xb;
      static const unsigned long b[] = {
        0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000L, 0xFFFFffff00000000L
      };
      static const unsigned int S[]  = { 1, 2, 4, 8, 16, 32 };
      unsigned int r = 0;
    
    #define STEP(i)   \
      if(v & b[i]) {  \
        int t = S[i]; \
        v >>= t;      \
        r  |= t;      \
      }
      STEP(5)
      STEP(4)
      STEP(3)
      STEP(2)
      STEP(1)
      STEP(0)
      return r;
    }
    
    int main(int ac, char **av) {
      return printf("%d\n", tsb(TO_L(av[1]), TO_L(av[2]))), 0;
    }
    

    I think this implements your algorithm and it’s very fast, needing only 6 steps. See this great source of bit twiddling hacks.

    so ross$ ./a.out 1f f
    4
    so ross$ ./a.out 471234abcdabcd 981234abcdabcd
    55
    so ross$ ./a.out 1deadbeef 7feedface
    34
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to do operations like compare that two number arrays have the same
in bash I need to compare two float numbers, one which I define in
I need to compare two account number columns from two different tables to see
i need to compare two objects but compare a number of their properties in
I need to compare two Date s (e.g. date1 and date2 ) and come
I need to compare two bitmaps. One bitmap is loaded from a file, the
When you need to compare two tables to see what the differences are, are
Note: Near complete beginner to logic programming I need to compare two lists of
could anyone assist me with this. I need to compare two beefy xml files
Possible Duplicate: Using jQuery to compare two arrays I need a Javascript or jQuery

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.