awk
A vanilla no-frills adaptation of the Horner's Polynomial Evaluation Method to IPv4 addresses. But unlike the standard notation, I prefer placing all the base scalars on one side, so all the coefficients could be placed sequentially adjacent one another.
echo '192.168.1.1
10.10.104.36
8.8.8.8' |
awk -F. '$0=_*(_*(_*$1+$2)+$3)+$4' \_=256
3232235777
168454180
134744072
If you're curious, arithmetic expression (and bit-wise-ops only variant) expands out to -
256 * (256 * (256 * (192) + 168) + 1) + 1
1 | (1 | (168 | (192) << 8) << 8) << 8
Personally I prefer the arithmetic expression because you can see the 4 components of the address exactly in the same order as its appears in dot-(.
) form, but requires them to be written in little-endian order in order to conform with the preference of isolating scalars of the base versus the coefficients instead of having them chaotically interweaved.
The parens (
)
around both 192
are absolutely superfluous. I only included them for completeness and clarity of the illustration.