views:

444

answers:

4

I'm working with IPv6 addresses in the form:

FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF

Internally, I store them in an array:

TIp6Bytes = array [0..15] of Byte;

I need to manipulate the IPv6 addresses in a number of ways including adding, dividing, multiplying etc. Can anyone suggest a good way to do this?

I guess I should have mentioned that I'm working with Delphi 2009

A: 

I dont have an answer, but by $deity im curious: Why do you want to do this?

Visage
That ought to be a comment on the question
James L
There are several reasons, one of them is that I need to split subnets into child subnets and need do divide the address to find out where the child subnet boundaries lie.
norgepaul
A: 

I would say that if you can add, you can then use it to subtract, multiply and divide using addition. Should I assume overflows will be simply ignored?

I seem to recall a method of adding bit-oriented variables using XOR. I am looking for that answer now.

Hopefully, this will point you in the right direction. If I can find that XOR code, I will post it for you.

Here it is: Bitwise operation Exclusive disjunction is often used for bitwise operations. Examples: 1 xor 1 = 0 1 xor 0 = 1 1110 xor 1001 = 0111 (this is equivalent to addition without carry)

And the reference is: http://www.absoluteastronomy.com/topics/Exclusive_disjunction

Rodddgers
+4  A: 

Jes Klinke wrote a bignum unit for Pascal here.

Disclaimer : I have not used this library personally.

Marshall Fryman
Wow... handles Int128, Int256 ...all the way up to Int3k plus a few just in case. :) Nice find, it looks like it would solve the problem well, although I have not dug too deep into the pieces yet. (+1 wish I could give more)
skamradt
I tried using this library, but it has a major bug in the subtraction function. I tried to contact the author, but have not received a reply. See my own answer to this question for another option.
norgepaul
+2  A: 

After trying many of the suggestions I could not find a library that fulfilled all my needs and were bug free. I searched a little harder and found a relatively new library by Alex Ciobanu which does BigIntegers (and Big Cardinals) seamlessly allowing you to manipulate them in much the same way as you manipulate normal Integers, Cardinals etc.

As well as BigIntegers, the library also provides a number of very useful features. From the readme:

  • A set of generic collections classes (List, Dictionary, HashSet, etc).
  • Date/Time functionality all combined in a few structures (somehow equivalent to .NET's DateTime structure)
  • Type Support concept that defines a set of default "support classes" for each built-in Delphi types (used as defaults in collections). Custom "type support" classes can be registered for your custom data types.
  • BigCardinal and BigInteger data types.
  • Smart pointers in Delphi

The library is being actively developed. In fact, the author fixed a small bug I found within a day.

You can read more about the library on Alex's blog and download DeHL from Google code.

norgepaul