Repository navigation
Add IPAddr#to_bits - #131
Merged
Merged
Conversation
Returns the binary representation of the address value as a string of "0" and "1" characters, right adjusted to the address size (32 characters for IPv4, 128 for IPv6).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request adds
IPAddr#to_bits, which returns a string containing the binary representation of the address (32 characters for IPv4, 128 for IPv6).Use case
When designing an IP address allocation scheme for an organization, there are
cases where you want to keep addresses human-readable while making sure the
scheme fits within the available bits. In such cases, having the address as a
binary string makes it easy to check the bit patterns while designing.
For example, consider a scheme based on 172.16.0.0/16:
(e.g. management / tenant / reserved).
Splitting that string according to the scheme above:
Why not
to_i.to_s(2)?IPAddr#to_i.to_s(2)drops leading zeros, so the caller has to pad the resultto the address width (32 for IPv4, 128 for IPv6). That means code that handles
both families has to branch on the address family.
to_bitshides this andprovides a common interface for IPv4 and IPv6:
Related