Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Friday Programming Challenge

Friday Programming Challenge

Scheduled Pinned Locked Moved The Lounge
databasetutorialcareerlearning
13 Posts 9 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P PIEBALDconsult

    The other day, in the course of my job, I was presented with the following challenge. Given an IP address (IPv4 or IPv6) and the "number of set leading bits of the subnet mask", generate the actual subnet mask. For example, given an IPv4 address and the value 16, produce 255.255.0.0 . There are thirty-three such subnet masks for IPv4 and one hundred twenty-nine for IPv6. I, of course, created a look-up table (in a database) rather than generating the mask each time, but I still had to write the code to do it (so I did). Your challenge, should you choose to accept it, is to do likewise in a language of your choice, or at least describe a technique.

    V Offline
    V Offline
    V 0
    wrote on last edited by
    #2

    Hardcode it in a library of course ! :rolleyes:

    V.
    (MQOTD rules and previous solutions)

    1 Reply Last reply
    0
    • P PIEBALDconsult

      The other day, in the course of my job, I was presented with the following challenge. Given an IP address (IPv4 or IPv6) and the "number of set leading bits of the subnet mask", generate the actual subnet mask. For example, given an IPv4 address and the value 16, produce 255.255.0.0 . There are thirty-three such subnet masks for IPv4 and one hundred twenty-nine for IPv6. I, of course, created a look-up table (in a database) rather than generating the mask each time, but I still had to write the code to do it (so I did). Your challenge, should you choose to accept it, is to do likewise in a language of your choice, or at least describe a technique.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #3

      Wow. That's hard. On my old computer I would have set up two 16 bit registers to hold the result and a counter, initialized with 32. As long as the counter is <= the length of the mask, I would (over both registers) bitwise shift in a 1, otherwise a 0. Loop ends when 32 shifts have been done. Expanding this to IPv6 would require a third register (luckily the CPU has 16 of them) and the counter would have to be initialized to 48, otherwise everything remains the same. The machine code for this would be about 20 - 30 bytes long, even if this is an ancient 8 bit CPU. Back then this problem would have been a basic (no pun intended) exercise.

      The language is JavaScript. that of Mordor, which I will not utter here
      This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
      "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

      L 1 Reply Last reply
      0
      • P PIEBALDconsult

        The other day, in the course of my job, I was presented with the following challenge. Given an IP address (IPv4 or IPv6) and the "number of set leading bits of the subnet mask", generate the actual subnet mask. For example, given an IPv4 address and the value 16, produce 255.255.0.0 . There are thirty-three such subnet masks for IPv4 and one hundred twenty-nine for IPv6. I, of course, created a look-up table (in a database) rather than generating the mask each time, but I still had to write the code to do it (so I did). Your challenge, should you choose to accept it, is to do likewise in a language of your choice, or at least describe a technique.

        J Offline
        J Offline
        Joan M
        wrote on last edited by
        #4

        I'm 100% sure that the best method would be starting the windows settings programmatically and then send messages to the right buttons and IP controls to set it to automatically calculate the right value, then just copy the result in a memory variable, it's up to you to close the open windows again... Of course you should implement that thinking in all Windows versions. Easy, fast, always correct and working with only a little help of the Spy++ tool. :thumbsup: PS: I think today it will be a great programming day... :rolleyes: Have a nice weekend! :thumbsup:

        [www.tamautomation.com] | Robots, CNC and PLC machines for grinding and polishing. [YouTube channel]

        1 Reply Last reply
        0
        • P PIEBALDconsult

          The other day, in the course of my job, I was presented with the following challenge. Given an IP address (IPv4 or IPv6) and the "number of set leading bits of the subnet mask", generate the actual subnet mask. For example, given an IPv4 address and the value 16, produce 255.255.0.0 . There are thirty-three such subnet masks for IPv4 and one hundred twenty-nine for IPv6. I, of course, created a look-up table (in a database) rather than generating the mask each time, but I still had to write the code to do it (so I did). Your challenge, should you choose to accept it, is to do likewise in a language of your choice, or at least describe a technique.

          C Offline
          C Offline
          C3D1
          wrote on last edited by
          #5

          Just to make sure i'm understanding everything right. You want to convert CIDR notation to dotted decimal?! My approach is to convert the CIDR to a binary string (eg. /8 = 11111111000000000000000000000000), then split this string to the octets in an array (eg. [0]=11111111 [1]=00000000 [2]=00000000 [3]=00000000) and then converting each octet to decimal. Sample source in C#:

          public enum IP_TYPE
          {
          IPv4
          , IPv6
          }

          public String convertCIDR2DottedDecimal(int nCIDR, IP_TYPE eType)
          {
          // Build Binary String
          int nBits = (eType == IP_TYPE.IPv4) ? 32 : 128;
          String sBinary = "";

          for(int nIdx = nCIDR - 1; nIdx >= 0; nIdx--)
          	sBinary += "1";
          
          for(int nIdx = nBits - nCIDR; nIdx >= 0; nIdx--)
          	sBinary += "0";
          
          // Split Binary String to Octets
          int nOctets = (eType == IP\_TYPE.IPv4) ? 4 : 16;
          String\[\] aOctets = new String\[nOctets\];
          
          for(int nIdx = 0; nIdx < nOctets; nIdx++)
          {
          	String sOctet = "";
          	for(int nOctetIdx = 0; nOctetIdx < 8; nOctetIdx++)
          	{
          		sOctet += sBinary\[nOctetIdx\];
          	}
          
          	sBinary = sBinary.Substring(8);
          
          	// Convert each Octet to Decimal
          	aOctets\[nIdx\] = "" + Convert.ToInt32(aOctets\[nIdx\], 2);
          }
          
          return String.Join(".", aOctets);
          

          }

          1 Reply Last reply
          0
          • P PIEBALDconsult

            The other day, in the course of my job, I was presented with the following challenge. Given an IP address (IPv4 or IPv6) and the "number of set leading bits of the subnet mask", generate the actual subnet mask. For example, given an IPv4 address and the value 16, produce 255.255.0.0 . There are thirty-three such subnet masks for IPv4 and one hundred twenty-nine for IPv6. I, of course, created a look-up table (in a database) rather than generating the mask each time, but I still had to write the code to do it (so I did). Your challenge, should you choose to accept it, is to do likewise in a language of your choice, or at least describe a technique.

            T Offline
            T Offline
            Thomas Daniels
            wrote on last edited by
            #6

            I've built one for IPv4 in C#. I was working on IPv6, but then I got stuck because .NET doesn't have UInt128, and couldn't be bothered anymore to look for alternatives ;P.

            string subnetMaskIPv4(int bits)
            {
            uint max = 4294967295;
            uint mask = max << (32 - bits);
            byte[] maskBytes = BitConverter.GetBytes(mask);
            Array.Reverse(maskBytes);
            return String.Join(".", maskBytes);
            }

            The quick red ProgramFOX jumps right over the Lazy<Dog>.

            1 Reply Last reply
            0
            • P PIEBALDconsult

              The other day, in the course of my job, I was presented with the following challenge. Given an IP address (IPv4 or IPv6) and the "number of set leading bits of the subnet mask", generate the actual subnet mask. For example, given an IPv4 address and the value 16, produce 255.255.0.0 . There are thirty-three such subnet masks for IPv4 and one hundred twenty-nine for IPv6. I, of course, created a look-up table (in a database) rather than generating the mask each time, but I still had to write the code to do it (so I did). Your challenge, should you choose to accept it, is to do likewise in a language of your choice, or at least describe a technique.

              M Offline
              M Offline
              Mark_Wallace
              wrote on last edited by
              #7

              42

              I wanna be a eunuchs developer! Pass me a bread knife!

              1 Reply Last reply
              0
              • L Lost User

                Wow. That's hard. On my old computer I would have set up two 16 bit registers to hold the result and a counter, initialized with 32. As long as the counter is <= the length of the mask, I would (over both registers) bitwise shift in a 1, otherwise a 0. Loop ends when 32 shifts have been done. Expanding this to IPv6 would require a third register (luckily the CPU has 16 of them) and the counter would have to be initialized to 48, otherwise everything remains the same. The machine code for this would be about 20 - 30 bytes long, even if this is an ancient 8 bit CPU. Back then this problem would have been a basic (no pun intended) exercise.

                The language is JavaScript. that of Mordor, which I will not utter here
                This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
                "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #8

                CDP1802 wrote:

                The machine code for this would be about 20 - 30 bytes long

                And in the modern way of doing things, this is how far that gets you.. :laugh: Ah well, that's progress!

                [Test]
                public voi

                How do you know so much about swallows? Well, you have to know these things when you're a king, you know.

                L 1 Reply Last reply
                0
                • L Lost User

                  CDP1802 wrote:

                  The machine code for this would be about 20 - 30 bytes long

                  And in the modern way of doing things, this is how far that gets you.. :laugh: Ah well, that's progress!

                  [Test]
                  public voi

                  How do you know so much about swallows? Well, you have to know these things when you're a king, you know.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #9

                  Except for the shift instruction with carry, I would still know all instructions and could do it right now. The bosses would probably not see the importance, so it may have to wait.

                  The language is JavaScript. that of Mordor, which I will not utter here
                  This is Javascript. If you put big wheels and a racing stripe on a golf cart, it's still a fucking golf cart.
                  "I don't know, extraterrestrial?" "You mean like from space?" "No, from Canada." If software development were a circus, we would all be the clowns.

                  1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    The other day, in the course of my job, I was presented with the following challenge. Given an IP address (IPv4 or IPv6) and the "number of set leading bits of the subnet mask", generate the actual subnet mask. For example, given an IPv4 address and the value 16, produce 255.255.0.0 . There are thirty-three such subnet masks for IPv4 and one hundred twenty-nine for IPv6. I, of course, created a look-up table (in a database) rather than generating the mask each time, but I still had to write the code to do it (so I did). Your challenge, should you choose to accept it, is to do likewise in a language of your choice, or at least describe a technique.

                    O Offline
                    O Offline
                    Orjan Westin
                    wrote on last edited by
                    #10

                    I'd ask you to write a CP article about the solution you coded, and download the included source code from that article. ;-)

                    P 1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      The other day, in the course of my job, I was presented with the following challenge. Given an IP address (IPv4 or IPv6) and the "number of set leading bits of the subnet mask", generate the actual subnet mask. For example, given an IPv4 address and the value 16, produce 255.255.0.0 . There are thirty-three such subnet masks for IPv4 and one hundred twenty-nine for IPv6. I, of course, created a look-up table (in a database) rather than generating the mask each time, but I still had to write the code to do it (so I did). Your challenge, should you choose to accept it, is to do likewise in a language of your choice, or at least describe a technique.

                      F Offline
                      F Offline
                      Freak30
                      wrote on last edited by
                      #11

                      I only did it for IPv4 because I'm not familiar with the IPv6 format and too lazy to look it up. ;P

                      if (bits > 31)
                      throw too many bits blah
                      unsigned char buffer[4];
                      int i = 0;
                      for (; bits >= 8; i++)
                      {
                      buffer[i] = 255;
                      bits -= 8;
                      }
                      if (bits > 0)
                      {
                      unsigned char ch = 255;
                      buffer[i++] = ch << (8 - bits)
                      }
                      for (; i < 4; i++)
                      buffer[i] = 0;

                      char result[16];
                      sprintf(result, "%c.%c.%c.%c", buffer[0], buffer[1], buffer[2], buffer[3]);

                      The good thing about pessimism is, that you are always either right or pleasently surprised.

                      P 1 Reply Last reply
                      0
                      • O Orjan Westin

                        I'd ask you to write a CP article about the solution you coded, and download the included source code from that article. ;-)

                        P Offline
                        P Offline
                        PIEBALDconsult
                        wrote on last edited by
                        #12

                        Ah. Step 0) Invent a time machine. Step 1) Go forward an hour. Step 2) Copy the code. Step 3) Go back an hour. Step 4) Paste the code. Done.

                        1 Reply Last reply
                        0
                        • F Freak30

                          I only did it for IPv4 because I'm not familiar with the IPv6 format and too lazy to look it up. ;P

                          if (bits > 31)
                          throw too many bits blah
                          unsigned char buffer[4];
                          int i = 0;
                          for (; bits >= 8; i++)
                          {
                          buffer[i] = 255;
                          bits -= 8;
                          }
                          if (bits > 0)
                          {
                          unsigned char ch = 255;
                          buffer[i++] = ch << (8 - bits)
                          }
                          for (; i < 4; i++)
                          buffer[i] = 0;

                          char result[16];
                          sprintf(result, "%c.%c.%c.%c", buffer[0], buffer[1], buffer[2], buffer[3]);

                          The good thing about pessimism is, that you are always either right or pleasently surprised.

                          P Offline
                          P Offline
                          PIEBALDconsult
                          wrote on last edited by
                          #13

                          I started with IPv4 as well and then simply hacked it into an IPv6 solution. :-D

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups