Available in all subroutines.

Extracts bit_count bits (at most 32) starting with the bit number start_bit from the given IPv4 or IPv6 address and return them in the form of a non-negative integer.

Bit numbering starts at 0 from the right-most end of the address (the lowest order bit in the last byte of the address is bit number 0). As this function extracts bits from the address, it copies them to form the integer. In the address from which it extracts bits, the lowest order bit extracted from the first byte (the right-most byte) will be copied to the lowest order bit in the resulting integer.

If this function goes past the highest order bit in the left-most byte in the address before completing the copying of bit_count bits, then it will leave the remaining high-order bits in the integer at zero.

The bit count can be, at most, 32. The start bit must be lower than 128. The bit count plus start bit must be, at most, 128. If the VCL using this function violates any of these three constraints, then it will be rejected at compilation time.

The start bit and bit count must be constant values.

IPv6 addresses are 128 bits long and IPv4 addresses are 32 bits long. This function behaves as if an IPv4 address were padded with zeros on the left to 128 bits. If this function is applied to an address that is neither IPv4 nor IPv6, then it will return 0.


if (addr.extract_bits(server.ip, 0, 8) == 7) {
/* received on an IPv4 address that ends in ".7" or an IPv6 address that ends in "07" */

Try it out

addr.extract_bits is used in the following solution recipes, which show real world use cases. Click RUN to provision a Fastly service, execute the code on Fastly, and see how the function behaves.

Anonymize client IP for logging

Strip the last octet or compute a hash of client IP address for anonymization.

User contributed notes

We welcome comments that add use cases, ideas, tips, and caveats. All comments will be moderated before publication. To post support questions, visit our support center and we'll find you the help you need.