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

  • Home
  • SEARCH
  • 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 3401314
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:00:00+00:00 2026-05-18T05:00:00+00:00

HI, How to do Bitwise AND(&) on CString values in MFC(VC++)? Example : CString

  • 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”

  • 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-05-18T05:00:00+00:00Added an answer on May 18, 2026 at 5:00 am

    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).

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

Sidebar

Related Questions

I am confused as to when I should use Boolean vs bitwise operators and
I need to compute a number (a/(2**b) using only bitwise operators such as !
How is the conditional operator represented using bitwise operators? It is a homework question
I want to create a nice cryptography using bitwise operators. However I fail to
The task is to implement a bit count logic using only bitwise operators. I
I found some exercises where you combine n-bit 2's complement values in different ways
After seeing this question , my first thought was that it'd be trivial to
So at work yesterday, I had to write an application to count the pages
A gotcha I've run into a few times in C-like languages is this: original
This optimzation question has been bugging me for the last day. In my program

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.