Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #define _CRT_SECURE_NO_WARNINGS // Perdonami, Windows :C
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. typedef unsigned char byte;
  7.  
  8. typedef union
  9. {
  10.     struct
  11.     {
  12.         byte b1, b2, b3, b4;
  13.     } bytes;
  14.  
  15.     unsigned int asUInt;
  16. } IPv4Address;
  17.  
  18. int main(int argc, char* argv[])
  19. {
  20.     IPv4Address IP, SubnetMask, BroadcastAddress;
  21.  
  22.     // Un po' di pulizia...
  23.     IP.asUInt = SubnetMask.asUInt = BroadcastAddress.asUInt = 0;
  24.  
  25.     // Il formato di scanf sa tanto di ridicolo XD
  26.     // Giusto per renderlo piĆ¹ leggibile:
  27.     // hh = char, u = unsigned.
  28.     printf("Inserisci un indirizzo IP nel formato a.b.c.d: ");
  29.     scanf("%hhu.%hhu.%hhu.%hhu",    &(IP.bytes.b1),
  30.                     &(IP.bytes.b2),
  31.                     &(IP.bytes.b3),
  32.                     &(IP.bytes.b4)
  33.                 );
  34.  
  35.     printf("Inserisci la maschera di sottorete nel formato a.b.c.d: ");
  36.     scanf("%hhu.%hhu.%hhu.%hhu",    &(SubnetMask.bytes.b1),
  37.                     &(SubnetMask.bytes.b2),
  38.                     &(SubnetMask.bytes.b3),
  39.                     &(SubnetMask.bytes.b4)
  40.                 );
  41.  
  42.     // E ora, la sacra danza dei bit :O
  43.     BroadcastAddress.asUInt = IP.asUInt | ~SubnetMask.asUInt;
  44.  
  45.     // Output
  46.     printf("\nIndirizzo di broadcast: %hhu.%hhu.%hhu.%hhu\n",   BroadcastAddress.bytes.b1,
  47.                                     BroadcastAddress.bytes.b2,
  48.                                     BroadcastAddress.bytes.b3,
  49.                                     BroadcastAddress.bytes.b4
  50.                                 );
  51.  
  52.     return 0;
  53. }