Skip to main content

Stack Exchange Network

Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

IPv4 Integer Conversion Function

Write the shortest function to convert an IP address into its integer representation and output it as an integer.

To change an IPv4 address to its integer representation, the following calculation is required:

  • Break the IP address into its four octets.
  • (Octet1 * 16777216) + (Octet2 * 65536) + (Octet3 * 256) + (Octet4)

Sample Input

192.168.1.1           10.10.104.36           8.8.8.8

Sample Output

3232235777            168454180              134744072

Answer*

Cancel
2
  • \$\begingroup\$ Can shave seven easily with sum(int(j)<<i*8for i,j in enumerate(input().split('.')[::-1])), removing an unnecessary space before the for (though modern Python emits a SyntaxWarning for that), and using shifting+multiplication instead of multiplication+exponentiation avoids more terms and parentheses. \$\endgroup\$
    ShadowRanger
    –  ShadowRanger
    2025-10-15 17:47:17 +00:00
    Commented yesterday
  • \$\begingroup\$ By using 8*(3-i) as factor and dropping the reversing slice [::-1], you get rid of the warning and save bytes. Taking input() would require print() output though. Better just use an anonymous lambda. \$\endgroup\$
    movatica
    –  movatica
    2025-10-15 23:07:50 +00:00
    Commented yesterday

Morty Proxy This is a proxified and sanitized view of the page, visit original site.