Options

IP address addition

certhelpcerthelp Member Posts: 191
If we could add IP addresses ( only IPv4), adding 192.168.1.1 and 10.1.1.1 gives 202.169.2.2:
    
[FONT=lucida console]    192.168.1.1
 +  010.001.1.1
    -----------
    202.169.2.2

[/FONT]

How would you handle octets greater than 254? Cary over to next octet?
    
[FONT=lucida console]    192.168.1.1
 +  010.100.1.1
    -----------
    192.[B]268[/B].2.2

[/FONT]

Comments

  • Options
    LizanoLizano Member Posts: 230 ■■■□□□□□□□
    Its been a while but dont you need to convert each octet to binary first, then add them as binary and then change it back?
  • Options
    TrifidwTrifidw Member Posts: 281
    What will the purpose be again? *Confused*
  • Options
    certhelpcerthelp Member Posts: 191
    There doesn't seem to be any practical use, as far as I can tell. It's just a question to test some sort of skills, I guess.icon_confused.gif:
  • Options
    CodeBloxCodeBlox Member Posts: 1,363 ■■■■□□□□□□
    There is no real point to doing this. But I think you'd carry any bits over to the next octet that make the value more than 255 for any single octet. An IP address is only 32 bits and if you have an octet with a value of 268, that requires at least 9 bits. That breaks IP addressing.

    Edit: I am referring to IPv4 in this post
    Currently reading: Network Warrior, Unix Network Programming by Richard Stevens
  • Options
    certhelpcerthelp Member Posts: 191
    Lizano thanks for the reply. But binary is just representation, adding in binary or decimal would give the same numbers for the octets which could be greater than 255.

         184.168.185.085 => 10111000.10101000.10111001.01010101
    +    064.202.189.170 => 01000000.11001010.10111101.10101010
        -----------------    -----------------------------------
         248.370.374.255 => 11111000.101110010.101110110.11111111
     
    
       11111000.101110010.101110110.11111111 => 248.370.374.255
    


    So, I can add the octets but don't know what to do with invalid octets greater than 255. icon_smile.gif I could subtract the octets greater than 255 by 256 and use the reminder. Any other options?
  • Options
    certhelpcerthelp Member Posts: 191
    Thanks CodeBlox.
  • Options
    VAHokie56VAHokie56 Member Posts: 783
    icon_scratch.gificon_scratch.gif....but why?
    .ιlι..ιlι.
    CISCO
    "A flute without holes, is not a flute. A donut without a hole, is a Danish" - Ty Webb
    Reading:NX-OS and Cisco Nexus Switching: Next-Generation Data Center Architectures
  • Options
    certhelpcerthelp Member Posts: 191
    VAHokie56 wrote: »
    icon_scratch.gificon_scratch.gif....but why?

    I don't know either. I will certainly ask the person who asked me if I get a chance. :)
  • Options
    jamesp1983jamesp1983 Member Posts: 2,475 ■■■■□□□□□□
    VAHokie56 wrote: »
    icon_scratch.gificon_scratch.gif....but why?

    x2, but I would think carry over to next octet.
    "Check both the destination and return path when a route fails." "Switches create a network. Routers connect networks."
  • Options
    certhelpcerthelp Member Posts: 191
    jamesp1983 wrote: »
    x2, but I would think carry over to next octet.

    You mean carry over to the left octet starting from right most octet. Correct? Since the left most octect has nothing left of it to carry over, it should be dropped?
  • Options
    CodeBloxCodeBlox Member Posts: 1,363 ■■■■□□□□□□
    That's what I'm saying. Start from the least significant bits ( rightmost ) and work your way left.
    Currently reading: Network Warrior, Unix Network Programming by Richard Stevens
  • Options
    certhelpcerthelp Member Posts: 191
    CodeBlox wrote: »
    That's what I'm saying. Start from the least significant bits ( rightmost ) and work your way left.

    Thanks.
  • Options
    certhelpcerthelp Member Posts: 191
    Strange...I can convert the dotted quad IPs to IP decimal and add them to get the new IP decimal number, then convert back to dotted quad IP. But, the conversion tools (Decimal/Dotted-Quad Conversion) available on internet only give me
    4185093887 = 249.115.118.255
    Which means, they aren't carrying over the excess from each octet. So, the carry over isn't used? icon_confused.gif:
  • Options
    CodeBloxCodeBlox Member Posts: 1,363 ■■■■□□□□□□
    The tool probably wasn't created to account for adding IP addresses together. My best assumption would be to carry over still. Even then, at the most significant octet ( the leftmost ) you'd likely run into the problem of too high of a number and would end up discarding excess bits (lack of a better word) there.
    Currently reading: Network Warrior, Unix Network Programming by Richard Stevens
  • Options
    JDMurrayJDMurray Admin Posts: 13,039 Admin
    Why don't you treat the IP address as what it is, a 32-bit unsigned integer, and use a (binary/decimal/hexadecimal) calculator to add two 32-bit integers together?

    And then you can figure out a useful application for this exercise: icon_scratch.gif+icon_scratch.gif=icon_scratch.gif
  • Options
    CodeBloxCodeBlox Member Posts: 1,363 ■■■■□□□□□□
    Anything greater than 4294967295 that is unsigned will always be 255.255.255.255 or it will start over at 0.0.0.0

    Edited the post. Adding IP Addresses (as useless as it is ) does work. You end up with a broadcast address if the number is high enough. That or you'll get back to 0.0.0.0
    Currently reading: Network Warrior, Unix Network Programming by Richard Stevens
  • Options
    certhelpcerthelp Member Posts: 191
    CodeBlox wrote: »
    The tool probably wasn't created to account for adding IP addresses together. My best assumption would be to carry over still. Even then, at the most significant octet ( the leftmost ) you'd likely run into the problem of too high of a number and would end up discarding excess bits (lack of a better word) there.

    I think I need to write a tool to convert from dotted quad to decimal and back if I need to handle carry over.
  • Options
    CodeBloxCodeBlox Member Posts: 1,363 ■■■■□□□□□□
    I believe the whole thing about bits carrying over is true. I'm thinking about the whole thing in binary. I did some calculations and look at what I got:
     16843263 = 1.1.1.255
    +16843009 = 1.1.1.1
    =====================
     33686272 = 2.2.3.0
    

    And if your number were high enough, you'd end up with an IP of 255.255.255.255 OR it'd start over at 0.0.0.0 and you'd get a totally useless IP address.

    Thats what I'm sticking with here ;)

    Think about it in binary... You will see.
    Currently reading: Network Warrior, Unix Network Programming by Richard Stevens
  • Options
    certhelpcerthelp Member Posts: 191
    CodeBlox wrote: »
    And if your number were high enough, you'd end up with an IP of 255.255.255.255

    Thats what I'm sticking with here ;)

    Think about it in binary... You will see.

    Yes it is true. The tools I referenced beefore also use carry over. That's why they both give me 249.115.118.255 not 248.114.118.255.

    I used 8589934590 (which is 255.255.255.255 + 255.255.255.255) to convert to dotted quad and one of the above tools gives me 255.255.255 while the other gives 511.255.255.254. icon_smile.gif
  • Options
    certhelpcerthelp Member Posts: 191
    CodeBlox wrote: »
    Anything greater than 4294967295 that is unsigned will always be 255.255.255.255

    Edited the post. Adding IP Addresses (as useless as it is ) does work. You end up with a broadcast address if the number is high enough.

    Yes. That should be true even if the addition doesn't have any usefulness practically speaking.
  • Options
    LizanoLizano Member Posts: 230 ■■■□□□□□□□
    certhelp wrote: »
    Lizano thanks for the reply. But binary is just representation, adding in binary or decimal would give the same numbers for the octets which could be greater than 255.

    Lol, that´s was pretty dumb of me wasn´t it. I didn´t really think that one through, I was thinking of ANDing.
  • Options
    certhelpcerthelp Member Posts: 191
    Lizano wrote: »
    Lol, that´s was pretty dumb of me wasn´t it. I didn´t really think that one through, I was thinking of ANDing.

    No, it isn't dumb, just confusion. I have confused it myself sometimes. icon_smile.gif
Sign In or Register to comment.