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 8449669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:46:28+00:00 2026-06-10T10:46:28+00:00

I have two bytes that are made up of two 4-bit numbers packed together.

  • 0

I have two bytes that are made up of two 4-bit numbers packed together. I need to know if either of the two numbers from the first byte matches either of the numbers from the second byte. Zero is considered null and shouldn’t match itself.

Obviously, I can do this by unpacking the numbers and comparing them one by one:

a = 0b10100101;
b = 0b01011111; // should have a match because 0101 is in both

a1 = a>>4; a2 = a&15;
b1 = b>>4; b2 = b&15;

return (a1 != 0 && (a1 == b1 || a1 == b2)) || (a2 != 0 && (a2 == b1 || a2 == b2));

//     ( true   && (  false  ||   false )) || ( true   && (  true   ||   false ))
//     ( true   &&         false         ) || ( true   &&         true          )
//            false                        ||         true
//                                        TRUE

However I’m just wondering if anyone knows of a cleaner way to do this?

  • 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-06-10T10:46:29+00:00Added an answer on June 10, 2026 at 10:46 am

    A cleaner way would be to get rid of that hard-to-parse expression and make the code more readable.

    def sameNybble (a, b):
        # Get high and low nybbles.
    
        ahi = (a >> 4) & 15 ; alo = a & 15;
        bhi = (b >> 4) & 15 ; blo = b & 15;
    
        # Only check ahi if non-zero, then check against bhi/blo
    
        if ahi != 0:
            if ahi == bhi or ahi == blo:
                return true
    
        # Only check alo if non-zero, then check against bhi/blo
    
        if alo != 0:
            if alo == bhi or alo == blo:
                return true
    
        # No match
    
        return false
    

    Any decent optimising compiler will basically give you the same underlying code so it’s sometimes better to optimise for readability.

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

Sidebar

Related Questions

I have made a lookup table that allows you to blend two single-byte channels
I have a ushort that consists of two bytes. 15 14 13 12 11
I have a list of bytes that represent raw samples read in from an
I have a 16 bit luminance value stored in two bytes, and I want
I have two simple programs that use named pipes, here is the first: int
I need to perform a bitwise equality between two bytes. That means that for
I have made a program in Java that calculates powers of two, but it
I have a Java BigInteger containing two bytes (ex: 1000000100110111). I would like to
I have two byte arrays in C# using .NET 3.0. What is the most
If I have two byte[] arrays, is there a built-in function to compare them

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.