Thread: Anyone got a faster way of doing this?

  1. #1
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250

    Question Anyone got a faster way of doing this?

    so I have this code... which seems long and cumbersome, and not all that fun to type out, and so far I have thought of using tables to get things up and running, but I wanted to see if anyone had something better it would go something like this

    I am trying to find the bits borrowed out of an ip address and the bits remaining

    Code:
    void bitsborrowed(int ip[], int subnet[], char addressclass,  int *bitsborrowed, int *bitsremaning)
    {
        if(addressclass == 'c')
        {
            //for class c use the last octet
            if(subnet[3] == 252)
            {
                   *bitsremaining = 2;
                   *bitsborrowed = 6;
            }
            else if(subnet[3] == 248)
            {
                   *bitsremaining = 3;
                   *bitsborrowed = 5;
            }
         
    
      //etc...
      if(addressclass == 'b')
      {
          if(subnet[3] === 252 && subnet[2] == 255)
          {
                 *bitsremaining = 2;
                 *bitsborrowed = 14;
          }
    
    
    
       // and so forth...
    as you can see there are a lot of cases to check... any help would be great, thanks!

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    One solution is to implement all cases in their own functions and use a map.

    Kuphryn

  3. #3
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250

    Question

    Hmmm, well thats possible, I was thinking would it be possible to use it as such? I know the total range of values, would it be easier to create sort of a hash. That is, create an array that has values which represent the differences between the octets? Like I could figure things based on that? I have no idea, just more ideas would be awesome... partially because a lot of you have good ideas and I like to see as many as I can.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    34
    Wow

    Is that repetitve or what?

    Perhaps you may want to look at using switch and increment or decrement your bits...

    Based on the octet value

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    First, represent the ip as an unsigned char array since the max value of any # is 255 (or 0xFF). Next, do this exercise which will help solve your problem:

    Write a function to count the number of bits in an unsigned char:
    Code:
    int count_bits(usigned char b);
    Hint: you'll want to do something in a loop with a counter from 1 to 8 using the bitshift operator (<<). Write something up, and if you're still stuck, post that code and we'll continue from there.

    gg

  6. #6
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250

    Well...

    Well I got one part of the code implemented, but for some reason I am getting segmentation faults everytime I run the code in a specific function... I've isolated it to this place



    (header provided for reference)
    Code:
    void dtob(int oc3[], int oc2[], int oc1[], int third, int second, int first, int numoc)
    when I run this code (the arrays are predefined as being arrays of 8 zeros)

    Code:
    int oc1[] = {0,0,0,0,0,0,0,0};
    int oc2[] = {0,0,0,0,0,0,0,0};
    int oc3[] = {0,0,0,0,0,0,0,0};
    
    dtob(oc1,oc2,oc3,sn[3],sn[2],sn[1], 3);
    it gets a segmentation fault before it hits the function...
    sn[3], sn[2], and sn[1] are defined and have the values of 0, 0, and 240

    anyone see something obvious?? I apparently can't see it, and don't understand why it is doing this...


    any help would be awesome, thanks again.

  7. #7
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    doh, to clarify as soon as that function call takes place there is a segmentation fault...

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    The code location of the seg fault and the cause are rarely the same. Most likely, something is wrong upstream. You are going to have to narrow it down some more and post more code.
    Also, know that a seg fault means you are accessing memory that don't belong to you.

    gg

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Is the code as above, or have you changed it since posting that?
    [edit]Oh, I see its a different function, better post it.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250

    Post

    Ok here's the code... forgive the horrid coding style I am just typing it quick was gonna clean it up later once I got the code plastered out...and yes I should use things like reading in chars and whatnot... I know

    getting minimal sleep and trying to do this just ain't cutting it


    Code:
    void input(int a[], int b[], char *);
    //void printstep(int);
    void bitsborrowed(int [], int *, int *, char);
    void conversion(int [], int *, int *, int);
    void dtob(int [], int [], int [], int, int, int, int);
    
    int main(void)
    {
            int ipaddress[MAX_OCTECT];
            int subnetmask[MAX_OCTECT];
            int bitsborrow = 0;
            int bitsremain = 0;
            char addressclass;
    
            input(ipaddress, subnetmask, &addressclass);
    
            if(addressclass == 'n')
            {
                cout << "This is a class I do not support." << endl;
                return 0;
            }
    
            cout << endl << addressclass << endl;
            bitsborrowed(subnetmask, &bitsborrow, &bitsremain, addressclass);
            cout << endl << endl << "bitsborrowed " << bitsborrow << " bitsremain " << bi
    tsremain;
            cout << endl << "These are the numbers for this subnet mask." << endl;
            return 0;
    }
    
    
    
    void input(int ip[], int subnet[], char *adcl)
    {
    
            cout << "Please enter an ip address, using spaces between each octet. : ";
            cin >> ip[0] >> ip[1] >> ip[2] >> ip[3];
    
            cout << endl;
    
            cout << "You entered: ";
            for(int i = 0; i < 4; i++)
            {
                    cout << ip[i];
             if( i < 3)
                            cout << ".";
            }
            cout << endl;
            cout << "Please enter the subnet mask using spaces between each octet. : ";
            cin >> subnet[0] >> subnet[1] >> subnet[2] >> subnet[3];
    
            cout << endl;
    
            for(int j = 0; j < 4; j++)
            {
                    cout << subnet[j];
                    if(j < 3)
                            cout << ".";
            }
    
            if(0 < ip[0] && 127 >= ip[0])
            {
               *adcl = 'a';
            }
            else if(128 <= ip[0] && ip[0] <= 191)
            {
                *adcl = 'b';
            }
            else if(192 <= ip[0] && 223 >= ip[0])
            {
                *adcl = 'c';
            }
            else
            {
                *adcl = 'n';
            }
    
    }
    
    void bitsborrowed(int subnet[], int *bitsborrowed, int *bitsremaining, char aclass)
    {
        int octet = 0;
        if(aclass == 'c')
        {
            conversion(subnet, bitsborrowed, bitsremaining, 1);
        }
    
        else if(aclass == 'b')
        {
            conversion(subnet, bitsborrowed, bitsremaining, 2);
        }
        else
        {
            conversion(subnet, bitsborrowed, bitsremaining, 3);
        }
    }
    
    
    
    void conversion(int sn[], int *bb, int *br, int octets)
    {
        /*int oc1[] = {0,0,0,0,0,0,0,0};
        int oc2[] = {0,0,0,0,0,0,0,0};
        int oc3[] = {0,0,0,0,0,0,0,0};
        */
        int oc1[8];
        int oc2[8];
        int oc3[8];
    
        int tempbr = 0;
        int tempr = 0;
    
        if(octets == 1)
        {
            dtob(oc1,oc2,oc3,sn[3],0,0,1);
    
            int i = 7;
            while(oc1[i] == 1)
            {
                tempbr++;
                i--;
            }
            tempr = 8 - tempbr;
    
            *bb = tempbr;
            *br = tempr;
        }
        if(octets == 2)
        {
            dtob(oc1,oc2,oc3,sn[3],sn[2],0,2);
            int i = 7;
            while(oc2[i] == 1)
            {
                tempbr++;
                i--;
            }
            if(i == 0)
            {
    
                i = 7;
                while(oc1[i] == 1)
                {
                    tempbr++;
                    i--;
                }
    tempr = 16 - tempbr;
                *bb = tempbr;
                *br = tempr;
            }
            else
            {
                tempr = 16-tempbr;
                *bb = tempbr;
                *br = tempr;
            }
        }
    
        if(octets == 3)
        {
            cout << "Made it." << endl;
            cout << sn[3] << " " << sn[2] << " " << sn[1] << " " << endl;
            for(int j = 0; j < 7; j++)
            {
                cout << oc1[j] << " " << oc2[j] << " " << oc3[j] << endl;
            }
            dtob(oc3, oc2, oc1, sn[3], sn[2], sn[1], 3);
            cout << "Made it again." << endl;
            int i = 7;
            while(oc3[i] == 1)
            {
                tempbr++;
                i--;
            }
            if(i == 0)
            {
                i = 7;
                while(oc2[i] == 1)
                {
                    tempbr++;
                    i--;
                }
    
            }
            else
            {
                tempr = 16-tempbr;
                *bb = tempbr;
                *br = tempr;
            }
            if(i == 0)
            {
                i = 7;
                while(oc1[i] == 1)
                {
                    tempbr++;
    i--;
                }
    
                tempr = 24 - tempbr;
                *bb = tempbr;
                *br = tempr;
            }
    
        }
    }
    
    void dtob(int oc3[], int oc2[], int oc1[], int third, int second, int first, int numo
    c)
    {
        int remainder = 0;
        int count = 0;
    
        cout << "123";
        if(numoc >= 1)
        {
            cout << "OK!" << third;
            if(third != 0)
            {
            while(third != 1)
            {
                remainder = third % 2;      //calculate the remainder
                third = (int)(third / 2);
                oc3[count++] = remainder;
            }
            oc3[count] = third;
            cout << "First!";
            }
            else
            {
                for(int i = 0; i < 7; i++)
                {
                    oc3[i] = 0;
                }
                cout << "FIRST!";
            }
        }
        if(numoc >= 2)
        {
            count = 0;
            while(second != 1)
            {
                remainder = second % 2;      //calculate the remainder
                second = (int)(second / 2);
                oc2[count++] = remainder;
     }
            oc2[count] = second;
            cout << "Second!";
        }
        if(numoc == 3)
        {
            count = 0;
            while(first != 1)
            {
                remainder = first % 2;
                first = (int)(first / 2);
                oc1[count++] = remainder;
            }
            oc1[count] = first;
            cout << "Third!";
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Faster bitwise operator
    By Yarin in forum C++ Programming
    Replies: 18
    Last Post: 04-29-2009, 01:56 PM
  2. Faster way of printing to the screen
    By cacophonix in forum C Programming
    Replies: 16
    Last Post: 02-04-2009, 01:18 PM
  3. Which Operation is Faster "=" or "+=" ?
    By thetinman in forum C++ Programming
    Replies: 37
    Last Post: 06-06-2007, 07:29 PM
  4. does const make functions faster?
    By MathFan in forum C++ Programming
    Replies: 7
    Last Post: 04-25-2005, 09:03 AM
  5. Floating point faster than fixed-point
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2001, 11:34 PM