import struct
import socket


def subnet_check(netmask_bits, ip_address, target):
    """
    Check if the specific gateway is within the network that the IP and Mask
    represents.

    netmask_bits:
         Number of bits in the netmask

    ip_address:
         A dotted quad string with any IP in the subnet to be checked

    target:
         A dotted quad string representing the target to check

    Returns True only if the specified target is within the calculated subnet
    range
    """
    ip_decimal = struct.unpack('!L', socket.inet_aton(ip_address))[0]
    target_decimal = struct.unpack('!L', socket.inet_aton(target))[0]
    netmask_decimal = (0xFFFFFFFF >> int(netmask_bits)) ^ 0xFFFFFFFF
    return ip_decimal & netmask_decimal == target_decimal & netmask_decimal