Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1111 1111 1111 1111 1111 1111 1000 0000
- octet 1 . octet 2 . octet 3 . octet 4
- 0000 0000 0000 0000 0000 0000 0000 0000
- 1111 1111 1111 1111 1111 1111 1000 0000
- 255 . 255 . 255 . 128
- In bytes: 128.2.19.0
- In binary 10000000 00000010 00010011 00000000
- The bitmask: 11111111 11111111 11111111 10000000
- Ergo: ------- network ------------ host
- #include <stdio.h>
- #include <arpa/inet.h>
- uint32_t cidr_to_netmask(uint8_t cidr)
- {
- uint8_t unset_bits = 32 - cidr;
- return ntohl(0xffffffff << unset_bits);
- }
- int main(void)
- {
- uint8_t cidr = 25;
- uint32_t _netmask = cidr_to_netmask(cidr);
- struct in_addr _netmask_addr = { _netmask };
- char netmask[16];
- if (inet_ntop(AF_INET, (struct in_addr *)&_netmask_addr, (char *)&netmask, sizeof(netmask)) == NULL) {
- fprintf(stderr, "error.n");
- return 1;
- }
- printf("%d = %sn", cidr, netmask);
- return 0;
- }
- 25 = 255.255.255.128
Add Comment
Please, Sign In to add comment