0

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"

Swapnil Gupta
  • 8,751
  • 15
  • 57
  • 75

1 Answers1

5

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 a CString then passing it to the inet_addr() function.

A (simple) example based on your code snippet.

CString NASServerIP = "172.24.15.25";
CString  SystemIP = " 142.25.24.85";
CString strSubnetMask = "255.255.255.0";

// CStrings can be casted into LPCSTRs (assuming the CStrings are not Unicode)
unsigned long NASServerIPBin = inet_addr((LPCSTR)NASServerIP);
unsigned long SystemIPBin = inet_addr((LPCSTR)SystemIP);
unsigned long strSubnetMaskBin = inet_addr((LPCSTR)strSubnetMask);

// Now, do whatever is needed on the unsigned longs.
int result1 = NASServerIPBin & strSubnetMaskBin;
int result2 = SystemIPBin & strSubnetMaskBin;

if(result1==result2)
{
    cout << "Both in Same network";
}
else
{
    cout << "Not in same network";
}

The bytes in the unsigned longs are "reversed" from the string representation. For example, if your IP address string is 192.168.1.1, the resulting binary from inet_addr would be 0x0101a8c0, where:

  • 0x01 = 1
  • 0x01 = 1
  • 0xa8 = 168
  • 0xc0 = 192

This shouldn't affect your bitwise operations, however.

You of course need to include the WinSock header (#include <windows.h> is usually sufficient, since it includes winsock.h) and link against the WinSock library (wsock32.lib if you're including winsock.h).

Community
  • 1
  • 1
In silico
  • 51,091
  • 10
  • 150
  • 143
  • @In silico : Pls provide some sample. – Swapnil Gupta Nov 22 '10 at 09:44
  • @Swapnil Gupta: It should be easy to figure out based on the `inet_addr()` documentation and the Stack Overflow question/answer I linked. However, I've added a few lines. – In silico Nov 22 '10 at 09:57
  • @In silico : Using this approach i can find out whether the two IP Addresses are in same network or not ? – Swapnil Gupta Nov 22 '10 at 10:09
  • @Swapnil Gupta: I woudn't suggest using `inet_addr` if it's useless for what you want to do in your question. – In silico Nov 22 '10 at 10:12
  • @In silico: Then what will be the gud approach for doing this ? – Swapnil Gupta Nov 22 '10 at 10:15
  • @Swapnil Gupta: What I meant is that you can use `inet_addr()` to do what you want to do. The problem you're having in your question is that you need to obtain binary representations of the IP address strings, and `inet_addr()` can do that. Once you get the IP addresses in binary, you can then use the bitwise operations. I've edited my answer to include a usage example based on the code snippet you provided. – In silico Nov 22 '10 at 10:18
  • @Swapnil Gupta: No problem. If you have another question, feel free to post it here on Stack Overflow, as long as it is specific and well-phrased. – In silico Nov 22 '10 at 10:25
  • @In silico : For Ip Address 172.24.17.174 i am getting the resulting binary from inet_addr as 2920356012.Can u pls explain how i am getting this value ? – Swapnil Gupta Nov 22 '10 at 10:47
  • @Swapnil Gupta: `2920356012` is the correct decimal value for the binary form of the IP address string `172.24.17.174`. That is, `2920356012 == 0xae1118ac == 172.24.17.174`. So the conversion is in fact correct and is working. – In silico Nov 22 '10 at 10:57
  • @In silico : Yes Silico , it is right but i just want to know how we are getting this value.What is the format that is converting it this format ? – Swapnil Gupta Nov 22 '10 at 11:01
  • @Swapnil Gupta: It makes more sense if you look at the IP address via hexadecimal. Each group of numbers in the IP address string represents a byte. So the IP address `172.24.17.174` is a sequence of these bytes: `172`, `24`, `17`, and `174`. Convert each byte to their hexadecimal form, and you get `0xac`, `0x18`, `0x11`, and `0xae`, respectively. If you put these four bytes together in reverse order, you get `0xae1118ac`. That is the number `2920356012` in hexadecimal. – In silico Nov 22 '10 at 11:05