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

  • SEARCH
  • Home
  • 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 7447489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:37:05+00:00 2026-05-29T12:37:05+00:00

Given two unsigned integers, what is the fastest way to count the number of

  • 0

Given two unsigned integers, what is the fastest way to count the number of matching digits in their base 4 representation?

example 1:

A= 13 = (31) in base 4

B= 15 = (33) in base 4

the number of matching digits in base 4 is 1.

example 2:

A= 163 = (2203) in base 4

B= 131 = (2003) in base 4

the number of matching digits in base 4 is 3.

The first step I guess is to calculate the bitwise XOR of the two integers, then we have to count number of 00 pairs ? what is the most efficient way t do that ?

note: assume that A and B have fixed number of digits in base 4, say exactly 16 digits.

  • 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-29T12:37:06+00:00Added an answer on May 29, 2026 at 12:37 pm

    Suppose, your ints are 4-byte each. 32 bits.

    The more understandable way:
    Help constant array:

    h[0]=3;
    for (int i=1; i<7; i++){
      h[i]=h[i-1]*4;
    }
    

    Later, for the check, if c is the integer after bitwise XOR :

    int count=0;
    for (int i=0; i<7; i++){
      if(c&h[i]==0)count++;
    }   
    

    Other solution. Obviously, faster, but a bit less understandable:

    int h[4]={1,0,0,0}
    
    int count=0;
    for (int i=0; i<15; i++){
      count+=h[c&3];
      c=c>>2;
    }   
    

    Further qickening:

    count= h[c&3] + h[(c=>>2)&3] + h[(c=>>2)&3] + h[(c=>>2)&3]+ h[(c=>>2)&3]+ h[(c=>>2)&3]+ h[(c=>>2)&3]+ h[(c=>>2)&3]+ h[(c=>>2)&3] + h[(c=>>2)&3]+ h[(c=>>2)&3]+ h[(c=>>2)&3]+ h[(c=>>2)&3]+ h[(c=>>2)&3]+ h[(c>>2)&3];
    

    Even further:

    int h[16]={2,1,1,1, 1,0,0,0, 1,0,0,0, 1,0,0,0};
    count= h[c&15] + h[(c=>>4)&15] + h[(c=>>4)&15] + h[(c=>>4)&15]  + h[(c=>>4)&15] + h[(c=>>4)&15] + h[(c=>>4)&15]+ h[(c>>4)&15];
    

    If you really need use the function so many (10^10) times, count h[256] (you already caught, how), and use:

    count= h[c&255] + h[(c=>>8)&255] + h[(c=>>8)&255] + h[(c>>8)&255] ;
    

    I think, the help array h[256*256] would be also usable yet. Then

    count= h[c&255] + h[(c>>16)&(256*256-1)];
    

    The array of 2^16 ints will be all in the processor cash (third level, though). So, the speed will be really great.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given two integers a and b , how would I go about calculating the
Given two interface references obtained from different sources. Is there a programmatic way to
Given two date ranges, what is the simplest or most efficient way to determine
Specifically: I have two unsigned integers (a,b) and I want to calculate (a*b)%UINT_MAX (UINT_MAX
Well, there are at least two low-level ways of determining whether a given number
I'm given a string hex_txt containing a 4 digit hex number in the way
Possible Duplicate: Addition of two chars produces int Given the following C++ code: unsigned
Given two sorted linked lists, L1 and L2, a solution to compute their intersection
Given two sets A and B, what is the common algorithm used to find
Given two 3D vectors A and B, I need to derive a rotation matrix

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.