6 Replies - 5273 Views - Last Post: 28 December 2011 - 07:48 PM Rate Topic: -----

#1 tabsang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-December 11

ANDing IP addr & Subnet Mask

Posted 28 December 2011 - 09:20 AM

Hi,

I am stuck with a peculiar problem.
I have an IP address in String format
String ip=”192.168.0.42”
(Binary value: 11000000.10101000.00000000.00101010)

I have a subnet mask in String format
String mask=”255.255.255.192”
(Binary value: 11111111. 11111111. 11111111.11000000)

Now, I have to perform a BITWISE AND operation on the addresses after converting them into binary and obtain the address that will be the network address in decimal format.

Please help!!!


I can use the below but it's too lengthy.
(Conv the IP addr into binary den the Mask into binary n den And em etc)
import java.net.*;
import java.util.*;


public class ipToBinary {
			public static void main(String[] args) {
		    String source = "192.168.1.1";
		    InetAddress ip = null;
		    try {
		        ip = InetAddress.getByName(source);
		    } catch (UnknownHostException e) {
		        e.printStackTrace();
		        return;
		    }
		    System.out.println( "source : " + ip);

		    // To bit sequence ------------
		    byte[] binaryIP = ip.getAddress();//raw IP address
		    
		    BitSet[] bitsets = new BitSet[binaryIP.length];
		    int k = 0;
		    System.out.print("to binary: ");
		    for (byte b : binaryIP) {
		        bitsets[k] = byteToBitSet(B)/>;
		        System.out.print( toString( bitsets[k] ) + ".");
		        k++;
		    }
		    System.out.println();

		    // Back to InetAdress ---------
		    byte[] binaryIP2 = new byte[4];
		    k = 0;
		    for (BitSet b : bitsets) {
		        binaryIP2[k] = bitSetToByte(B)/>;
		        k++;
		    }

		    InetAddress ip2 = null;
		    try {
		        ip2 = InetAddress.getByAddress(binaryIP2);
		    } catch (UnknownHostException e) {
		        e.printStackTrace();
		        return;
		    }

		    System.out.println( "flipped back to : " + ip2);
		    int x=11011;
		    int y=01101;
		    int z=x & y;
		    System.out.println(z);
		    
		}

		
		public static BitSet byteToBitSet(byte B)/> {
		    BitSet bits = new BitSet(8);
		    for (int i = 0; i < 8; i++) {
		        bits.set(i, ((b & (1 << i)) != 0) );
		    }
		    return bits;
		}

		
		public static byte bitSetToByte(BitSet bits) {
		    int value = 0;
		    for (int i = 0; i < 8; i++) {
		        if (bits.get(i) == true) {
		            value = value | (1 << i);
		        }
		    }
		    return (byte) value;
		}

		
		
		public static byte bitsToByte(boolean[] bits) {
		    int value = 0;
		    for (int i = 0; i < 8; i++) {
		        if (bits[i] == true) {
		            value = value | (1 << i);
		        }
		    }
		    return (byte) value;
		}

		
		
		public static boolean[] byteToBits(byte B)/> {
		    boolean[] bits = new boolean[8];
		    for (int i = 0; i < bits.length; i++) {
		        bits[i] = ((b & (1 << i)) != 0);
		    }
		    return bits;
		}

		
		
		public static String toString(BitSet bits){
		    String out = "";
		    for (int i = 0; i < 8; i++) {
		        out += bits.get(i)?"1":"0";         
		    }
		    return out;
		}
}


import java.net.*;
import java.util.*;

public class ipToBinary {
			public static void main(String[] args) {
		    String source = "192.168.1.1";
		    InetAddress ip = null;
		    try {
		        ip = InetAddress.getByName(source);
		    } catch (UnknownHostException e) {
		        e.printStackTrace();
		        return;
		    }
		    System.out.println( "source : " + ip);

		    // To bit sequence ------------
		    byte[] binaryIP = ip.getAddress();//raw IP address
		    
		    BitSet[] bitsets = new BitSet[binaryIP.length];
		    int k = 0;
		    System.out.print("to binary: ");
		    for (byte b : binaryIP) {
		        bitsets[k] = byteToBitSet(B)/>;
		        System.out.print( toString( bitsets[k] ) + ".");
		        k++;
		    }
		    System.out.println();

		    // Back to InetAdress ---------
		    byte[] binaryIP2 = new byte[4];
		    k = 0;
		    for (BitSet b : bitsets) {
		        binaryIP2[k] = bitSetToByte(B)/>;
		        k++;
		    }

		    InetAddress ip2 = null;
		    try {
		        ip2 = InetAddress.getByAddress(binaryIP2);
		    } catch (UnknownHostException e) {
		        e.printStackTrace();
		        return;
		    }

		    System.out.println( "flipped back to : " + ip2);
		    int x=11011;
		    int y=01101;
		    int z=x & y;
		    System.out.println(z);
		    
		}

		
		public static BitSet byteToBitSet(byte B)/> {
		    BitSet bits = new BitSet(8);
		    for (int i = 0; i < 8; i++) {
		        bits.set(i, ((b & (1 << i)) != 0) );
		    }
		    return bits;
		}

		
		public static byte bitSetToByte(BitSet bits) {
		    int value = 0;
		    for (int i = 0; i < 8; i++) {
		        if (bits.get(i) == true) {
		            value = value | (1 << i);
		        }
		    }
		    return (byte) value;
		}

		
		
		public static byte bitsToByte(boolean[] bits) {
		    int value = 0;
		    for (int i = 0; i < 8; i++) {
		        if (bits[i] == true) {
		            value = value | (1 << i);
		        }
		    }
		    return (byte) value;
		}

		
		
		public static boolean[] byteToBits(byte B)/> {
		    boolean[] bits = new boolean[8];
		    for (int i = 0; i < bits.length; i++) {
		        bits[i] = ((b & (1 << i)) != 0);
		    }
		    return bits;
		}

		
		
		public static String toString(BitSet bits){
		    String out = "";
		    for (int i = 0; i < 8; i++) {
		        out += bits.get(i)?"1":"0";         
		    }
		    return out;
		}
}






This post has been edited by modi123_1: 28 December 2011 - 09:27 AM
Reason for edit:: please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: ANDing IP addr & Subnet Mask

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2557
  • View blog
  • Posts: 10,673
  • Joined: 20-September 08

Re: ANDing IP addr & Subnet Mask

Posted 28 December 2011 - 09:41 AM

You can start off by converting each to long:

http://technojeeves....dress-to-number

Then you can make the body of the main method in my code the following:

	String ip="192.168.0.42";
	//(Binary value: 11000000.10101000.00000000.00101010)

	//I have a subnet mask in String format
	String mask="255.255.255.192";
	//(Binary value: 11111111. 11111111. 11111111.11000000)
	long ipL = IpConverter.ipToLong(ip);
	long maskL = IpConverter.ipToLong(mask);

	System.out.println(IpConverter.longToIp(ipL & maskL));


This post has been edited by g00se: 28 December 2011 - 09:46 AM

Was This Post Helpful? 1
  • +
  • -

#3 tabsang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-December 11

Re: ANDing IP addr & Subnet Mask

Posted 28 December 2011 - 09:55 AM

Please elaborate sir.......I tried. It didn't help :(
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2557
  • View blog
  • Posts: 10,673
  • Joined: 20-September 08

Re: ANDing IP addr & Subnet Mask

Posted 28 December 2011 - 10:01 AM

Quote

It didn't help


Did you run that code? If so, in what way does it differ from your expectation?
Was This Post Helpful? 0
  • +
  • -

#5 tabsang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-December 11

Re: ANDing IP addr & Subnet Mask

Posted 28 December 2011 - 11:57 AM

HURRAY. This Works just fine.
:D

import java.lang.String;

public class finalSubnet {

	public static void main(String args[]){
						
			String ipAddr="192.148.0.28";
			String mask="255.255.255.156";
			String networkAddr="";
			String[] ipAddrParts=ipAddr.split("\\.");
			String[] maskParts=mask.split("\\.");
						
			for(int i=0;i<4;i++){
				int x=Integer.parseInt(ipAddrParts[i]);
				int y=Integer.parseInt(maskParts[i]);
				int z=x&y;
				networkAddr+=z+".";
			}
			System.out.println("Address="+networkAddr);
		}
}



Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2557
  • View blog
  • Posts: 10,673
  • Joined: 20-September 08

Re: ANDing IP addr & Subnet Mask

Posted 28 December 2011 - 12:27 PM

Quote

This Works just fine.


The code i gave you earlier gives you exactly the same result (apart from a dot at the end of the ip address you get with your code - which is an error)

This post has been edited by g00se: 28 December 2011 - 12:27 PM

Was This Post Helpful? 1
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: ANDing IP addr & Subnet Mask

Posted 28 December 2011 - 07:48 PM

The & operator applies to any int (binary representation is just a representation)

Sheph wrote a Snippet about the bit wise operators
http://www.dreaminco...rators-in-java/

This post has been edited by pbl: 28 December 2011 - 08:20 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1