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 »
Sandbox

Comments on Find the IP address class [FINALIZED]

Post

Find the IP address class [FINALIZED]

+4
−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 ti 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 moderator attention?
You might want to add some details to your flag.

2 comment threads

Clarification questions (4 comments)
Just to be clear, this is just for IPv4, right? (2 comments)
Clarification questions
celtschk‭ wrote over 2 years ago

Does the code have to handle leading zeros (say, "010.000.007.023")? What about leading/trailing spaces (say, " 127.0.0.1 ")? Is the string guaranteed to be a valid IPv4 address? Will the IPv4 address always be given in point form, or does the code also need to handle the integer form (e.g. "167772161" is the same address as "10.0.0.1")?

Razetime‭ wrote over 2 years ago

I've edited the question to clarify these

celtschk‭ wrote over 2 years ago

Thanks. You didn't address the leading/trailing whitespace, though.

Razetime‭ wrote over 2 years ago

the string will always be a valid ip address, so there will be no whitespace.