Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Find the IP address class

+6
−0

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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

8 answers

+4
−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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+4
−0

Japt, 26 25 20 bytes

;Bg4mUn ¤Í¤ùT8 qT ÎÊ

Try it

-5 bytes thanks to @Shaggy

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

20 bytes (1 comment)
+3
−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
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+3
−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
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+2
−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!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Improvement: `i&1?4:3` can be changed to `i%2+3`. (2 comments)
+1
−0

Japt, 15 bytes

Port of m90's JS solution, until I manage to come up with something shorter.

;Bg7^Mm8|#ÿ^3îU

Try it

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−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
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+0
−0

C (gcc), 55 bytes

a;f(char*s){for(;atoi(s)&128>>a&&a<4;a++);return 65+a;}

Try it online!


Just call atoi and start masking with 0x80. If true, keep looping and next check with 0x40 and so on, until the mask either results in zero or 4 bits have been checked, meaning all bits in the ms nibble were set.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »