Determining if two IP adresses are on same subnet - is it leading or trailing 0s get dropped from IP address?

前端 未结 3 800
你的背包
你的背包 2021-01-01 07:59

I understand if two IP addresses are AND\'d with a subnet mask if the result is the same then they are on the same network. If the result is different then they are on diffe

相关标签:
3条回答
  • 2021-01-01 08:34

    IPv4 addreses are written as four ordinary decimal numbers (each fitting into a byte) separated by dots.

    The ordinary decimal number 1 is "one", which can also be written 001. However 100 is a different number, namely "a hundred".


    The AND operation is always a bitwise AND, so to understand it you must first see how the dotted-decimal address and netmask corresponds to a raw binary 32-bit address:

            126   .    127   .     0    .    1
         01111110   01111111   00000000   00000001
    
            255   .    128   .     0    .    0
    AND  11111111   10000000   00000000   00000000
     -----------------------------------------------
         01111110   00000000   00000000   00000000
            126   .      0   .     0    .    0
    

    So 126.127.0.1 with netmask 255.128.0.0 is in subnet 126.0.0.0/9

    In software one usually stores IPv4 addresses in a single 32-bit variable -- so 126.127.0.1 is 01111110011111110000000000000001 binary (which also encodes 2122252289 decimal, except that nobody ever cares what the decimal value of a 32-bit IP address is), and it is converted to dotted-decimal only when it needs to be shown to human users. This representation is what glglgl decribes as multiplying by 256 several times.

    If you also have the netmask in a 32-bit variable (11111111100000000000000000000000 binary or 4286578688 decimal), you can AND them in a single machine instruction to get the network address 01111110000000000000000000000000.

    0 讨论(0)
  • 2021-01-01 08:39

    Despite the fact that it's a rather odd question, I'll try answering your specific query:

    The number '1' in that second octet is really a number 1, and therefore it's '001', not '100'.

    That said, your example should have worked, so I suspect there's something wrong with your implementation. You should not need to worry about padding or anything. These are numbers, and bitwise-AND should "just work".

    You might want to provide more detail about what is going wrong.

    0 讨论(0)
  • 2021-01-01 08:56

    You are completely missing something: You don't just concat your digits together, because AND works on binary level.

    So either you process the IPv4 addresses byte-wise, or you transform them into a decimal number, but not with factor 1000, but with factor 256.

    This is to say, instead of doing

    ((126 * 1000 + 1) * 1000 + 0) * 1000 + 10 = 126001000010
    

    you should do

    ((126 * 256 + 1) * 256 + 0) * 256 + 10 = 2113994762
    

    . If you apply this to the other numbers, you should succeed.

    Alternatively, you could AND together the 4 numbers separately, so

    255 & 126 = 126 (in both cases), 128 & (1 resp. 127) = 0, 0 & (anything) = 0, 0 & (anything) = 0.
    

    So you get 126.0.0.0 for both, making them belong to the same subnet.

    0 讨论(0)
提交回复
热议问题