I am trying to write C# code that will determine the ipv6 prefix ('subnet' in IPv4) for an ipv6 address given in slash/ notation. As with routers, this requires a bitwise operation on an ipv6 address with its prefix length ('subnet mask' in ipv4).
for example: given 2000:1234::1234/64
the code will do:
(2000:1234::1234) AND (FFFF:FFFF:FFFF:FFFF) and will determine that the prefix for this address is 2000:1234:0000:0000
My difficulty doing this comes from the fact that unlike doing bitwise AND on two integers I have to do it on a byteArray or some other data structure. My idea was to convert the ipv6 address into a byte array using IpAddress.getAddressBytes() as well as convert the /prefix integer from the input into a byte array and do a bitwise AND on the two arrays.
Is this the right approach for solving this problem or should I look into other options such as using a 128bit integer library instead of using byte arrays?
Does anyone know from experience what would be the best approach to take?