Thread: requesting a logic for mask calculation

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    4

    requesting a logic for mask calculation

    Hi Guys,
    I have wrote a small logic below which will calculate a value called MASK via which I can calculate the starting and ending number
    This logic works fine if A = 0x14000000 B = 0x14000064
    Code:
    void main()
    {
    	unsigned long ulStartVal = 0x14000000;
    	unsigned long ulEndVal = 0x14000064;
    	unsigned long ulMask;
    	unsigned long ulTempVal;
    	// Calculate mask
    	ulMask = ~((ulStartVal) ^ (ulEndVal));
    	/* This step is kindoff mandatory startval AND operation with mask must give 
    	the value of startval again (value of ulTempVal and ulStartVal must be same) */
    	ulTempVal = ulStartVal & ulMask;
    	printf("%x %x",ulTempVal, ulMask);
    	//Endval calculation with startval and mask
    	ulEndVal  = ~((ulTempVal) ^ (ulMask));
    	printf("%x",ulEndVal);
    }
    But the above logic doesn't work fine if A = 0x1400000A B = 0x14000064 means if the value of A has last 8 bits set. C code or pseudo code or any suggestion on logic change is very much welcome.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Sorry, I don't understand: what is the purpose of this calculation?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    4
    its used to store starting ip address and derive ending ip address just with the help of mask..

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How do you derive the ending IP address given the starting IP address? For example, you gave the second example of A = 0x1400000A B = 0x14000064. How did you arrive at this?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2014
    Posts
    4
    This is as per configuration. Once a starting and ending IP address is given we need to store starting address ID and mask with the help of starting ID and mask calculate starting IP address and with the help of starting IP address and mask we need to calculate ending IP address

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Requesting C/c++ links for logic development.
    By CChakra in forum C Programming
    Replies: 2
    Last Post: 02-17-2014, 10:09 AM
  2. Requesting Some Guidance
    By London Fog in forum C Programming
    Replies: 4
    Last Post: 05-31-2009, 11:25 PM
  3. Requesting a challenge
    By RealityFusion in forum C++ Programming
    Replies: 8
    Last Post: 08-18-2003, 08:24 PM
  4. requesting html source from a server
    By threahdead in forum Linux Programming
    Replies: 2
    Last Post: 08-01-2003, 07:52 PM

Tags for this Thread