### Communities

tag:snake search within a tag
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
created:<1w created < 1 week ago
post_type:xxxx type of post
Challenges

# Find the IP address class

+4
−0

Given an IP address as a string, find its IP address class.

For reference, here is a table:

bits
Number
of networks
per network
in class
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.

Why does this post require moderator attention?
Why should this post be closed?

+3
−0

# Japt, 26 25 bytes

;Bg4mUqL În10,2 ùT8 qT Îl


Try it

Long and unwieldy, I'm rusty. Looks at the first 0 in the bitstring of the first octet.

Why does this post require moderator attention?

+2
−0

# JavaScript, 43 bytes

s=>"ABCDE"[Math.log2(s.slice(0,3)^255|8)^7]


Try it online!

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.

Why does this post require moderator attention?

+2
−0

# Python 3, 56 bytes

lambda x:chr(65+f'{int(float(x[:3]))&~8:08b}'.find('0'))


Try it online!

Takes in the ip address as a string and returns the classification letter.

## Explanation:

• x[:3] extracts the first 3 characters
• int(float(…)) converts str to int, passing through float to handle cases like 1.2.3.4 -> 1.2 -> 1
• …&~8 sets the 8 bit (fourth to last) of the integer to 0
• f'{…:08b}' converts the integer to a binary string, left-padded to 8 characters (e.g. 192 becomes '11000000' and 117 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
Why does this post require moderator attention?

+2
−0

# Vyxal, 24 bytes

\.€hEb:L8-0wẋf$J0ḟkA5Ẏ$i


Try it Online!

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 . and h gets the first of these part/octet
• E turns that into an integer and b 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 and 8- subtracts 8 from it
• 0w is the singleton list [0]
• ẋ then repeats the singleton list 8 - len(binary) times
• f flattens that list to get [0, 0, 0, 0, 0, 0, 0, 0]
• $ swaps this pad and the original list and J prepends the pad to the original • 0ḟ finds the index of the first 0 • kA is the uppercase alphabet and 5Ẏ gives the first 5 letters (ABCDE), our classes • $ swaps before i indexes into the string of classes using the index of 0
Why does this post require moderator attention?

+1
−0

# C (gcc), 64 62 bytes

Saved 2 bytes thanks to m90 in the comments.

i;f(char*s){i=atoi(s)>>4;return'A'+(i&8?i&4?i&2?i%2+3:2:1:0);}


Try it online!

Why does this post require moderator attention?

+0
−0

# 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

Why does this post require moderator attention?