The goal of this repository is to print a table with the bc calculator.
Print a table
The IP table
Print a table similar to this:
Mask | Binary | Decimal |
---|---|---|
/0 |
0000 0000 |
0 |
/1 |
1000 0000 |
128 |
/2 |
1100 0000 |
192 |
/3 |
1110 0000 |
224 |
/4 |
1111 0000 |
240 |
/5 |
1111 1000 |
248 |
/6 |
1111 1100 |
252 |
/7 |
1111 1110 |
254 |
/8 |
1111 1111 |
255 |
The adresses being written on 4 bytes, the table should look like the following :
Mask | Decimal |
---|---|
/24 |
255.255.255.0 |
/25 |
255.255.255.128 |
/26 |
255.255.255.192 |
/27 |
255.255.255.224 |
/28 |
255.255.255.240 |
/29 |
255.255.255.248 |
/30 |
255.255.255.252 |
/31 |
255.255.255.254 |
/32 |
255.255.255.255 |
Mask to decimal
\$decimal(mask) = 256 - 2 ^ ((8 - mask))\$
bc function
define void all_ranges() {
for ( index = 1; index < 8; ++index)
{
print "/", index, " - ", (256 - 2 ^ (8 - index)), "\n";
x = 0;
gap = 2 ^ (8 - index);
while (x < 256)
{
print "\t", x, " - ", x + gap - 1, "\n";
x = x + gap;
}
}
return ;
}
mask and byte
This function takes two arguments, a mask [1-8] and a byte [0-255], that print informations.
Example:
mask_byte(4, 42) mask : 4 byte : 42 mask: /4 -> decimal: 240 Network : 32 host min : 33 host max : 46 Broadcast: 47
define void mask_byte(mask, byte) {
print "mask : ", mask, "\n";
print "byte : ", byte, "\n";
gap = 2 ^ (8 - mask);
decimal = 256 - gap
print "mask: /", mask, " -> decimal: ", decimal, "\n";
network = 0;
while (network < 256)
{
broadcast = network + gap - 1;
if (byte <= broadcast)
break ;
network = network + gap;
}
if (mask == 8)
{
host_min = network;
host_max = broadcast;
}
if (mask != 8)
{
host_min = network + 1;
host_max = broadcast - 1;
}
print "Network : ", network, "\n";
print "host min : ", host_min, "\n";
print "host max : ", host_max, "\n";
print "Broadcast: ", broadcast, "\n\n";
return ;
}