HI,
How to do Bitwise AND(&) on CString values in MFC(VC++)?
Example :
CString NASServerIP = "172.24.15.25";
CString SystemIP = " 142.25.24.85";
CString strSubnetMask = "255.255.255.0";
int result1 = NASServerIP & strSubnetMask;
int result2 = SystemIP & strSubnetMask;
if(result1==result2)
{
cout << "Both in Same network";
}
else
{
cout << "not in same network";
}
How i can do bitwise AND on CString values ?
Its giving error as “‘CString’ does not define this operator or a conversion to a type acceptable to the predefined operator”
You don’t. Peforming a bitwise-AND on two strings doesn’t make a lot of sense. You need to obtain binary representations of the IP address strings, then you can perform whatever bitwise operations on them. This can be easily done by first obtaining a
const char*from aCStringthen passing it to theinet_addr()function.A (simple) example based on your code snippet.
The bytes in the
unsigned longsare “reversed” from the string representation. For example, if your IP address string is192.168.1.1, the resulting binary frominet_addrwould be0x0101a8c0, where:0x01=10x01=10xa8=1680xc0=192This shouldn’t affect your bitwise operations, however.
You of course need to include the WinSock header (
#include <windows.h>is usually sufficient, since it includeswinsock.h) and link against the WinSock library (wsock32.libif you’re includingwinsock.h).