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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:21:28+00:00 2026-05-26T13:21:28+00:00

Essentially, I have two 4 byte IP addresses: u_int32_t daddr; // in the packet

  • 0

Essentially, I have two 4 byte IP addresses:

u_int32_t daddr; // in the packet
u_int32_t entry; // in the forwarding table

I also have a prefix that goes with entry in the forwarding table:

unsigned short prefix; // in forwarding table corresponding to entry

I need to match the daddr to the entry based on the prefix. I am pretty sure what this means is: if e.g the prefix is 23, then I have to match the first 23 bits of the entry with the daddr. I honestly don’t even know where to start because I don’t know how to match individual bits.

I have a forwarding table with lots of entries which each have a different prefix. I am not sure how to match the da with the correct entry.. Any help would be much appreciated.
My daddr is stored in a standard ip header that I got from netinet ip.h file.

EDIT: I have find the “longest” match. So I am not comparing the entries to only check if they are equal, I am comparing them to determine how many bits are same. The best match is obviously when all of the bits are the same.

  • 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-26T13:21:29+00:00Added an answer on May 26, 2026 at 1:21 pm

    To compare only the top n bits of a and b of some unsigned type UInt:

    const unsigned int NBITS = sizeof(UInt) * CHAR_BIT;
    
    UInt a, b;
    
    if ((a >> (NBITS - n)) == (b >> (NBITS - n))) { /*...*/ }
    

    To compare the bottom m bits:

    if ((a << (NBITS - m)) == (b << (NBITS - m))) { /*...*/ }
    

    Some explanation: The type UInt has sizeof(UInt) bytes, and thus NBITS bits. To compare the top n bits, we simply shift both numbers to the right so that only n bits remain (the new top bits are filled in with zeros because the type is unsigned). To compare the bottom m bits, we shift both numbers to the left until all but m bits have fallen off the left (and zeros are filled in on the right):

    NBITS = 12, n = 4, m = 7:
    
         a:  1 2 3 4 x A B C D E F G
         b:  1 3 2 5 x A B C D E F G
    
    a >> 8:  0 0 0 0 0 0 0 0 1 2 3 4
    b >> 8:  0 0 0 0 0 0 0 0 1 3 2 5
    
    a << 5:  A B C D E F G 0 0 0 0 0
    b << 5:  A B C D E F G 0 0 0 0 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have two nodes of elements that I'm essentially trying to join. I
I have two tables containing data that are essentially headline lists from different sources.
I have what is essentially a shopping cart that needs to have two separate
I have two very similar classes that do essentially the same thing. The only
What I'm talking about here are nested classes. Essentially, I have two classes that
I have two essentially separate applications for configuring two pieces of hardware sold by
I have two variables. weekStartDate and startDate. they both hold essentially the same timestamp:
I have a table schema which is essentially a bunch of transaction info with
I have a very large, spread out class that is essentially a very contrived
I have two tables that model an inheritance relationship. This is expressed by a

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.