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

Post History

66%
+2 −0
Challenges Find the IP address class

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...

posted 2y ago by m90‭

Answer
#1: Initial revision by user avatar m90‭ · 2021-12-18T11:49:32Z (over 2 years ago)
# JavaScript, 43 bytes

<!-- language-all: lang-javascript -->

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

[Try it online!][TIO-kxbqkt5m]

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.

[TIO-kxbqkt5m]: https://tio.run/##VU7LCsIwELz3M3JqQZdkk7T1oODr6BcUC6VWrda2NCII/nvdJIrIMrAzuzPMpXgUphzq/j41fX2ohlvXXqvneJyPZr5gy9V6s2XZrrifoelOGBowTV1WIZ/IKEetX2mUJ/ux7FrTNZX9CbOAcXDDJgFLwY1dBSZOF56kECuQCCJ2XNOBc9Afn5gJoPwvvITkRlCWIJc2SkigJCegBEQkkBG5l5S9fuEkSV/a5qTa8xmgolRFLoVOIhrbMr4oalfqR/Vfrz3cij48RtH4Bg "JavaScript (SpiderMonkey) – Try It Online"