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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:30:29+00:00 2026-06-16T00:30:29+00:00

I need to extract the netmask for two sets of IPs and print it

  • 0

I need to extract the netmask for two sets of IPs and print it in CIDR format, out on to the screen. Thinking of using either shell or tcl to do this. Guess the major sticky points here is converting the hexadecimal netmask into CIDR format.

Tried a few different things, but does not seem to be working as expected.

  • 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-16T00:30:29+00:00Added an answer on June 16, 2026 at 12:30 am

    We can just write a function that iterates through the digits of the mask, adding up the number of bits set in each digit. There are only five legal digits in a CIDR-compliant netmask.

    Bash has the ${variable:offset} and ${variable:offset:length} substitutions for getting substrings of a variable, so it’s pretty easy to pick off the digits one at a time.

    function bitCountForMask {
        local -i count=0
        local mask="${1##0x}"
        local digit
    
        while [ "$mask" != "" ]; do
            digit="${mask:0:1}"
            mask="${mask:1}"
            case "$digit" in
                [fF]) count=count+4 ;;
                [eE]) count=count+3 ;;
                [cC]) count=count+2 ;;
                8) count=count+1 ;;
                0) ;;
                *)
                    echo 1>&2 "error: illegal digit $digit in netmask"
                    return 1
                    ;;
            esac
        done
    
        echo $count
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to extract the text between two HTML tags and store it in
I need to extract the path info using Path.GetFileName() , and this function doesn't
I need extract data from a JSON array, format '[[[1,2,3]],[[1,2,3],[1,2,3]],string]' in Qt. logically it's
I need to extract text from an HTML file using C#. I am trying
I need to extract the audio from a FLV file recorded using FMS. I
I need to extract the digits from the following string using regular expression: pc
I need to extract the first part of postcode for search calculation using a
I need to extract a string from a string. Only trouble is working out
I need to extract the full path to a file using regExp mydomain.com/path/to/file/myfile.html ->
I need to extract an icon from Windows Shell from a specific file extension.

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.