Find the IP address class
Task
Given an IP address as a string, find its IP address class.
For reference, here is a table:
Class | Leading bits |
Number of networks |
Addresses per network |
Total addresses in class |
Start address | End address |
---|---|---|---|---|---|---|
Class A | 0 | 128 (27) | 16,777,216 (224) | 2,147,483,648 (231) | 0.0.0.0 | 127.255.255.255 |
Class B | 10 | 16,384 (214) | 65,536 (216) | 1,073,741,824 (230) | 128.0.0.0 | 191.255.255.255 |
Class C | 110 | 2,097,152 (221) | 256 (28) | 536,870,912 (229) | 192.0.0.0 | 223.255.255.255 |
Class D (multicast) | 1110 | not defined | not defined | 268,435,456 (228) | 224.0.0.0 | 239.255.255.255 |
Class E (reserved) | 1111 | not defined | not defined | 268,435,456 (228) | 240.0.0.0 | 255.255.255.255 |
The simplest way to find the class of an address is to check the bits of its first octet. Once you find the the answer, you are required to output a character or a codepoint which represents one of the characters A, B, C, D or E.
- The given IP will always be valid, and it will always be a string.
- IP will not have insignificant zeroes.
- IP will not be in any form other than IPv4 dot notation.
Rules
This is code-golf. Shortest answer in each language wins.
JavaScript, 43 bytes …
3y ago
Japt, 26 25 bytes ;Bg4m …
3y ago
[Python 3], 56 bytes …
3y ago
Vyxal, 24 bytes ``` \.€hEb:L …
3y ago
Japt, 15 bytes Port of m90' …
9d ago
[C (gcc)], 64 62 bytes Save …
3y ago
J, 46 char ``` 'ABCDE'{:".{. …
2y ago
7 answers
JavaScript, 43 bytes
s=>"ABCDE"[Math.log2(s.slice(0,3)^255|8)^7]
Taking the first 3 characters of the string is always enough to include the first octet, while including at most one full stop.
Before the bitwise XOR, the substring is converted to a Number (if a full stop is present, it is interpreted as a decimal point) and then to a 32-bit integer (rounding towards zero), thus becoming the value of the first octet.
XORing with 255 produces 128–255 for class A, 64–127 for class B, 32–63 for class C, 16–31 for class D, and 0–15 for class E.
ORing with 8 narrows those ranges; in particular, class E is reduced to 8–15, which fits the pattern of the original ranges for the other classes.
Taking the base-2 logarithm, and (implicitly) rounding towards zero, produces 7, 6, 5, 4, 3 for classes A, B, C, D, E; XORing with 7 changes those to 0, 1, 2, 3, 4, which is finally used as an index into the string "ABCDE" to produce the result.
0 comment threads
Python 3, 56 bytes
lambda x:chr(65+f'{int(float(x[:3]))&~8:08b}'.find('0'))
Takes in the ip address as a string and returns the classification letter.
Explanation:
-
x[:3]
extracts the first 3 characters -
int(float(…))
convertsstr
toint
, passing throughfloat
to handle cases like1.2.3.4
->1.2
->1
-
…&~8
sets the 8 bit (fourth to last) of the integer to0
-
f'{…:08b}'
converts the integer to a binary string, left-padded to 8 characters (e.g.192
becomes'11000000'
and117
becomes'01110101'
) -
….find('0')
finds the index of the first'0'
character -
65+…
adds the character code for 'A' -
chr(…)
converts the sum from the previous step to a character
0 comment threads
Vyxal, 24 bytes
\.€hEb:L8-0wẋf$J0ḟkA5Ẏ$i
This is absolutely abysmal but whatever. A lot of bytes are taken up by trying to pad the list with zeroes (ẋ
was buggy or something so molding to 0 8ẋ
didn't work).
Explanation:
-
\.€
splits on.
andh
gets the first of these part/octet -
E
turns that into an integer andb
turns it into binary (list of 0/1) -
:
duplicates that list on the stack- The first copy is used to create a list of extra 0s to pad
-
L
gets the length of the list and8-
subtracts 8 from it -
0w
is the singleton list[0]
-
ẋ
then repeats the singleton list8 - len(binary)
times -
f
flattens that list to get[0, 0, 0, 0, 0, 0, 0, 0]
-
-
$
swaps this pad and the original list andJ
prepends the pad to the original
- The first copy is used to create a list of extra 0s to pad
-
0ḟ
finds the index of the first0
-
kA
is the uppercase alphabet and5Ẏ
gives the first 5 letters (ABCDE
), our classes -
$
swaps beforei
indexes into the string of classes using the index of0
0 comment threads
J, 46 char
'ABCDE'{~<:0 128 192 224 240 I.>:".{.}.;.1'.',
Sample runs
'ABCDE'{~<:0 128 192 224 240 I.>:".{.}.;.1'.', '127.255.255.255'
A
'ABCDE'{~<:0 128 192 224 240 I.>:".{.}.;.1'.', '191.255.255.255'
B
'ABCDE'{~<:0 128 192 224 240 I.>:".{.}.;.1'.', '223.255.255.255'
C
'ABCDE'{~<:0 128 192 224 240 I.>:".{.}.;.1'.', '239.255.255.255'
D
'ABCDE'{~<:0 128 192 224 240 I.>:".{.}.;.1'.', '255.255.255.255'
E
0 comment threads