Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8249593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:35:55+00:00 2026-06-07T23:35:55+00:00

In the example code below could someone walk through a more detailed explanation of

  • 0

In the example code below could someone walk through a more detailed explanation of exactly what the lines below are actually doing like you’d explain it to a beginning developer.

for (byte octet : octets) {
        result <<= 8;
        result |= octet & 0xff;
    }



public class Example {
public static long ipToLong(InetAddress ip) {
    byte[] octets = ip.getAddress();
    long result = 0;
    for (byte octet : octets) {
        result <<= 8;
        result |= octet & 0xff;
    }
    return result;
}

public static void main(String[] args) throws UnknownHostException {
    long ipLo = ipToLong(InetAddress.getByName("192.200.0.0"));
    long ipHi = ipToLong(InetAddress.getByName("192.255.0.0"));
    long ipToTest = ipToLong(InetAddress.getByName("192.200.3.0"));

    System.out.println(ipToTest >= ipLo && ipToTest <= ipHi);
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T23:35:56+00:00Added an answer on June 7, 2026 at 11:35 pm
    • byte[] octets = ip.getAddress(); -> stores entire IP address as byte array

    • for (byte octet : octets) {} -> splits the byte-array into octets and iterates over them

    • result <<= 8 (shorthand for result = result<<8) -> Left-shift 8 bits shifts the result in binary by 8 bits, and adds 8 trailing zeros. (Multiplies the value of result by 2^8)

    • result |= octet & 0xff; (same as result|=octet which is shorthand for result = result | or octect -> Bitwise ORing, same as addition in this case, because we have 8 zeros at the end of result, after the previous step.

    EDIT (thanks to @jtahlborn) -> the bitwise-anding with 0xFF is necessary to avoid sign extension when the byte is converted to an int.

    Example

    192.200.3.0 is the IP address in question. The final value is
    This is generated in the following manner

    192*(2^24) + 200*(2^16) + 3*(2^8) + 0*(2^0)
    3221225472 + 13107200 + 768 = 3234333440
    

    Now your code does the same, but using bitwise shifts
    192 in binary is 11000000. First, it is added to result = 0;
    result is now 11000000.
    Then it is shifted left by 8 bits (effectively multiplying it by 2^8)
    result is 11000000 00000000

    Now, binary value of 200, which is 11001000 is added, making result now 11000000 11001000
    This process is carried on, till you have the following 32 bit number,
    11000000 11001000 00000011 00000000 which translates to the same 3234333440

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the example source code below, is it possible for someone to see the
For the example below: if a == 100: # Five lines of code elif
Could someone please compile and execute the small sample code I provided below? Please
The example code below works as as a server process. But when I add
I know how to do this... I'll give example code below. But I can't
UPDATE 2011.09.13 This bug has been resolved by Adobe. The example code below now
For this code example below, usually I would use [self fall]; instead of the
For example, I have the java code below: URL u = new URL(http://google.com); URLConnection
I have a basic PHP question, take the code below for example, let's say
The example below shows the code I am using to test whether a user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.