My SQL-Server db stores IP netmasks as binary. I need a way to match these with a given IP
for example: is 192.168.21.5 part of a netmask which is stored in the db?
the binary representation of 192.168.21.5:
11000000.10101000.00010101.00000101 (without the dots)
Netmask stored in db is: binary(4) and a tinyint field
11000000.10101000.00010101.00000000 / 24 ( which would be: 192.168.21.0 /24 )
so the first 24 bits of 192.168.21.5 have to match a record in the database
How would I only check the first n bits of a binary field (similar to LEFT(text, 24)) ?
Is there any clever way to do this? maybe with bitwise AND ?
Thank you, Reinhard