Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to Perform Logical operation

How to Perform Logical operation

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestionhelp
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Shah Satish
    wrote on last edited by
    #1

    Hello In my application, i want to perform some logical operation.Ie "AND" operation, "OR"operation,"Exor" operation.. For example.. Enter value1 :-1011 Enter value2 :-1100. Answer is AND :- 1000 OR :- 1111 EXOR :- 0111 Any idea how can i get it..I am trying to Enter binary value as a string but then how to convert string to binary... Please help me..Any new logic ?? Thanks Shah Shah

    C C W 3 Replies Last reply
    0
    • S Shah Satish

      Hello In my application, i want to perform some logical operation.Ie "AND" operation, "OR"operation,"Exor" operation.. For example.. Enter value1 :-1011 Enter value2 :-1100. Answer is AND :- 1000 OR :- 1111 EXOR :- 0111 Any idea how can i get it..I am trying to Enter binary value as a string but then how to convert string to binary... Please help me..Any new logic ?? Thanks Shah Shah

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Shah Satish wrote:

      ..I am trying to Enter binary value as a string but then how to convert string to binary...

      Assuming you need to do that, use atoi to convert.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      C 1 Reply Last reply
      0
      • S Shah Satish

        Hello In my application, i want to perform some logical operation.Ie "AND" operation, "OR"operation,"Exor" operation.. For example.. Enter value1 :-1011 Enter value2 :-1100. Answer is AND :- 1000 OR :- 1111 EXOR :- 0111 Any idea how can i get it..I am trying to Enter binary value as a string but then how to convert string to binary... Please help me..Any new logic ?? Thanks Shah Shah

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        Shah Satish wrote:

        how to convert string to binary...

        Take a look at strtol[^]. You can provide 2 as the basis and it will convert your string representing a binary into a long. Then you just perform logical operations on your long (&& for AND, || for OR, for XOR I don't remember what it was but you can make an OR and then invert the result with !).


        Cédric Moonen Software developer
        Charting control [v1.2]

        S 1 Reply Last reply
        0
        • C Christian Graus

          Shah Satish wrote:

          ..I am trying to Enter binary value as a string but then how to convert string to binary...

          Assuming you need to do that, use atoi to convert.

          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          Christian Graus wrote:

          use atoi to convert.

          In that case, the string need to be in decimal representation, not in binary representation.


          Cédric Moonen Software developer
          Charting control [v1.2]

          1 Reply Last reply
          0
          • C Cedric Moonen

            Shah Satish wrote:

            how to convert string to binary...

            Take a look at strtol[^]. You can provide 2 as the basis and it will convert your string representing a binary into a long. Then you just perform logical operations on your long (&& for AND, || for OR, for XOR I don't remember what it was but you can make an OR and then invert the result with !).


            Cédric Moonen Software developer
            Charting control [v1.2]

            S Offline
            S Offline
            Shah Satish
            wrote on last edited by
            #5

            Thank you dear..I got my solution.

            1 Reply Last reply
            0
            • S Shah Satish

              Hello In my application, i want to perform some logical operation.Ie "AND" operation, "OR"operation,"Exor" operation.. For example.. Enter value1 :-1011 Enter value2 :-1100. Answer is AND :- 1000 OR :- 1111 EXOR :- 0111 Any idea how can i get it..I am trying to Enter binary value as a string but then how to convert string to binary... Please help me..Any new logic ?? Thanks Shah Shah

              W Offline
              W Offline
              Wim Engberts
              wrote on last edited by
              #6

              Hi Shah, Following routine should do the job: int StrToBinary (LPCTSTR szString) { int nBit = 1; int nResult = 0; int i; i = strlen (szString); while (i > 0) { if (szString[--i] != '0') nResult |= nBit; nBit *= 2; } return nResult; } Good luck William

              S 1 Reply Last reply
              0
              • W Wim Engberts

                Hi Shah, Following routine should do the job: int StrToBinary (LPCTSTR szString) { int nBit = 1; int nResult = 0; int i; i = strlen (szString); while (i > 0) { if (szString[--i] != '0') nResult |= nBit; nBit *= 2; } return nResult; } Good luck William

                S Offline
                S Offline
                Shah Satish
                wrote on last edited by
                #7

                Hi Wim..Thanks for your reply..

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups