Advertisement
VladislavSavvateev

Untitled

Feb 25th, 2021
568
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.59 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace MaskCalc {
  5.     internal static class Program {
  6.         public static void Main() {
  7.             if (!int.TryParse(Console.ReadLine(), out var maskVal) || maskVal < 0 || maskVal > 32) return;
  8.             Console.WriteLine(GenerateMask(maskVal));
  9.         }
  10.  
  11.         private static string GenerateMask(int val) {
  12.             var binMask = new string('1', val) + new string('0', 32 - val);
  13.             return string.Join(".", Enumerable.Range(0, 4)
  14.                 .Select(i => Convert.ToByte(binMask.Substring(i * 8, 8), 2)));
  15.         }
  16.     }
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement