I want to take an IP address routing prefix in CIDR [1] notation (i.e. the 24
in "192.168.100.14/24") and convert it into subnet form (i.e.
255.255.255.0). I came up with 4 ways to do that (see below), but none stand
out as best (although #3 and #4 seem a bit more straightforward as they
avoid the `max` temp). 

How would you go about it?

Here are the ways I came up with:
cidr := 24.
shift := 32 - cidr.
max := #[ 255 255 255 255 ] asInteger.
"1." ((max bitShift: shift negated) bitShift: shift) asByteArray.
"2." (max bitClear: (2 raisedTo: shift) - 1) asByteArray.
"3." ((2 raisedTo: shift) - 1) bitInvert32 asByteArray.
"4." (((2 raisedTo: cidr) - 1) bitShift: shift) asByteArray

1. https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing



-----
Cheers,
Sean
--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

Reply via email to