I'm trying to accomplish the following in C#/.NET 2.0:
Given an IPAddress object (say, 192.168.127.100) and another IPAddress object containing the IPv4Mask / subnet mask (say, 255.255.248.0), I should be able calculate the start and end of the IP Address range.
(Yes, I'm trying to do a for-loop thru a range of addresses on a subnet.)
Theoretically, I should be able to bitwise AND on the IPAddress and the SubnetMask to get the IPStart. Then I should be able to perform a bitwise XOR on the IPStart and the inverted (NOT'd) SubnetMask, to get the IPEnd.
The IPAddress object provides methods to output the address as a "long" or as "byte[]" (array of bytes).
Performing the bitwise operation on the long (because it is signed?) produces invalid results. And I can't seem to perform bitwise operation on the IPAddresses as an array of bytes.
EDIT-1: Okay, looping thru each byte in the array and performing bitwise AND, NOT, and XOR (in each respective situation) gets the right results.
The next problem I am running into is that I cannot perform the for-loop easily after converting the byte[] arrays into UInt32 or long. So, the first value works properly, but incrementing the uint/long by one makes the IPaddress increase from 192.168.127.0 to 193.168.127.0 -- It seems that after the byte[] array is transformed to uint/long, the order of the bytes gets reversed. So there is no easy way to increment from IPStart to IPEnd.
Any have any suggestions?