A simple way to understand it :
In IPv4:
Imagine a line of 256*256*256*256 (Or 2^32) possible IP adresses.
[] [] [] [] .................. [] [] []
256*256*256*256 total IP adresses
This has subnet mask 0.0.0.0 (or 0000 0000 0000 0000 0000 0000 0000 0000 in binary)
All the bits that are not masked can be used to give an IP adress in that network.
The possible adresses in that single net are:
0000 0000 0000 0000 0000 0000 0000 0000 (<-- NETMASK, not masking anything here...)
0000 0000 0000 0000 0000 0000 0000 0000 (IP 0.0.0.0)
to
1111 1111 1111 1111 1111 1111 1111 1111 (IP 255.255.255.255)
This whole network start at IP 0.0.0.0, and goes until IP 255.255.255.255
Each bit in a subnet mask will divide the line in 2 equal parts.
The first bit in a subnet mask will divide this in 2 equal parts, each with 128*256*256*256 (or 2^31) IP adresses :
[] [] [] .......... [] [] [] | [] [] ........... [] []
128*256*256*256 IP Adresses 128*256*256*256 IP Adr
This has subnet mask 128.0.0.0 (or 1000 0000 0000 0000 0000 0000 0000 0000 in binary)
All the bits that are not masked can be used to give an IP adress in that network.
So you can have 2 subnets, and for each subnets, you have 31 bits of available IP adresses.
For the first subnet (the one where, behind the netmask, is '0')
1000 0000 0000 0000 0000 0000 0000 0000 (<-- NETMASK)
0000 0000 0000 0000 0000 0000 0000 0000 (IP 0.0.0.0)
to
0111 1111 1111 1111 1111 1111 1111 1111 (IP 127.255.255.255)
and for the 2nd subnet (the one where, behind the netmask, is '1')
1000 0000 0000 0000 0000 0000 0000 0000 (<-- NETMASK)
1000 0000 0000 0000 0000 0000 0000 0000 (IP 128.0.0.0)
to
1111 1111 1111 1111 1111 1111 1111 1111 (IP 255.255.255.255)
The next additionnal bit in the subnet mask divides both sides in 2 equal parts of 2^30 IP Adresses each
And so on...
So if you try to assign, say, a subnet of /3, it means you spent 3 iteration dividing, ending up with 2^3=8 subnets.
Each subnet can only be one of the 8 subdivision of the whole line of machines. They can't overlap. Each one starting after the former one.
[] ... [] | [] ... [] | [] ... [] | [] ... [] | [] ... [] | [] ... [] | [] ... [] | [] ... []
32*256*256*256 or 2^30 IP Adresses each.
This has subnet mask 0.0.0.0
So for the first subnet (the one where, behind the netmask, is '000')
1110 0000 0000 0000 0000 0000 0000 0000 (<-- NETMASK)
0000 0000 0000 0000 0000 0000 0000 0000 (IP 0.0.0.0)
to
0001 1111 1111 1111 1111 1111 1111 1111 (IP 31.255.255.255)
and for the 2nd subnet (the one where, behind the netmask, is '001')
1110 0000 0000 0000 0000 0000 0000 0000 (NETMASK)
0010 0000 0000 0000 0000 0000 0000 0000 (IP 32.0.0.0)
to
0011 1111 1111 1111 1111 1111 1111 1111 (IP 63.255.255.255)
...
and for the 7th subnet (the one where, behind the netmask, is '110')
1110 0000 0000 0000 0000 0000 0000 0000 (NETMASK)
1100 0000 0000 0000 0000 0000 0000 0000 (IP 192.0.0.0)
to
1101 1111 1111 1111 1111 1111 1111 1111 (IP 223.255.255.255)
and for the 8nd subnet (the one where, behind the netmask, is '111')
1110 0000 0000 0000 0000 0000 0000 0000 (NETMASK)
1110 0000 0000 0000 0000 0000 0000 0000 (IP 224.0.0.0)
to
1111 1111 1111 1111 1111 1111 1111 1111 (IP 255.255.255.255)
IF you continue to add bit to the netmask, you continue divising: A subnet of /32 singles out a single machine.
But remember you can't really have only machines:
to make things work, some of the subnet's range are reserved:
for each subnet, the "0 bit at value 1" and "all bits at value 1" are usually reserved for broadcasting, so you usually have only nb_of_possible_adresses_in_the_subnet-2 IP adresses available in a subnet for actual machine interfaces. And one should better be the interface of a gateway which has another interface in other net(s), allowing you to use it as a gateway to reach those other nets (and everything, via those other net's gateways)