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. Operator Or

Operator Or

Scheduled Pinned Locked Moved C / C++ / MFC
11 Posts 5 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
    Schehaider_Aymen
    wrote on last edited by
    #1

    Hi, I'd like write a function doing sth like that.

    if(a == 5)
    val = val | 1000

    if( b == 20)
    val= val | 100

    in the end if i have the both condictions corrects i'd like to have 1100 as result on val. but i always do 1000. which operator should i use to have the 1100 as resut.

    "The Ultimate Limit Is Only Your Imagination."

    C A 2 Replies Last reply
    0
    • S Schehaider_Aymen

      Hi, I'd like write a function doing sth like that.

      if(a == 5)
      val = val | 1000

      if( b == 20)
      val= val | 100

      in the end if i have the both condictions corrects i'd like to have 1100 as result on val. but i always do 1000. which operator should i use to have the 1100 as resut.

      "The Ultimate Limit Is Only Your Imagination."

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      why are you using bitwise OR, if what you really want is addition ?

      image processing toolkits | batch image processing

      1 Reply Last reply
      0
      • S Schehaider_Aymen

        Hi, I'd like write a function doing sth like that.

        if(a == 5)
        val = val | 1000

        if( b == 20)
        val= val | 100

        in the end if i have the both condictions corrects i'd like to have 1100 as result on val. but i always do 1000. which operator should i use to have the 1100 as resut.

        "The Ultimate Limit Is Only Your Imagination."

        A Offline
        A Offline
        Aescleal
        wrote on last edited by
        #3

        Being slightly cynical addition sounds like the operator you neeed... With your statements val is going to end up containing 1004 as there's a large overlap in the bits set between the bit pattern for 1000 (1111101000b) and the bit pattern for 100 (1100100b). If you're trying to fiddle individual bits (i.e. turn bits with OR 1, turn them off with AND 0) then perhaps encode your numbers as powers of two and/or use hex:

        if( a == 5 )
        val |= 0x1000;

        if( b == 20 )
        val |= 0x100;

        Cheers, Ash PS: Another way of setting and resetting individual bits is to use bit fields. Perhaps they might be more in tune with what you want?

        S 2 Replies Last reply
        0
        • A Aescleal

          Being slightly cynical addition sounds like the operator you neeed... With your statements val is going to end up containing 1004 as there's a large overlap in the bits set between the bit pattern for 1000 (1111101000b) and the bit pattern for 100 (1100100b). If you're trying to fiddle individual bits (i.e. turn bits with OR 1, turn them off with AND 0) then perhaps encode your numbers as powers of two and/or use hex:

          if( a == 5 )
          val |= 0x1000;

          if( b == 20 )
          val |= 0x100;

          Cheers, Ash PS: Another way of setting and resetting individual bits is to use bit fields. Perhaps they might be more in tune with what you want?

          S Offline
          S Offline
          Schehaider_Aymen
          wrote on last edited by
          #4

          int blo=0;
          for(int i=0; i< New.GetLength(); i++ )
          {
          if(isalpha(New.GetAt(i)))
          {
          blo |=0x1000;
          if(isupper(New.GetAt(i))) //Upper case KeyStroke
          blo |= 0x100;
          else
          blo |=0x10; //Lower Case KeyStroke
          }
          }

          if i try it with Blood what will give me is 4352 or what i need is 1100 (1000 for being an alpha and 100 for being uppercase¨.

          "The Ultimate Limit Is Only Your Imagination."

          A D 2 Replies Last reply
          0
          • A Aescleal

            Being slightly cynical addition sounds like the operator you neeed... With your statements val is going to end up containing 1004 as there's a large overlap in the bits set between the bit pattern for 1000 (1111101000b) and the bit pattern for 100 (1100100b). If you're trying to fiddle individual bits (i.e. turn bits with OR 1, turn them off with AND 0) then perhaps encode your numbers as powers of two and/or use hex:

            if( a == 5 )
            val |= 0x1000;

            if( b == 20 )
            val |= 0x100;

            Cheers, Ash PS: Another way of setting and resetting individual bits is to use bit fields. Perhaps they might be more in tune with what you want?

            S Offline
            S Offline
            Schehaider_Aymen
            wrote on last edited by
            #5

            may be i need some var type instead of the int .... what kind of var should i use to hold the 11101110 (byte

            "The Ultimate Limit Is Only Your Imagination."

            1 Reply Last reply
            0
            • S Schehaider_Aymen

              int blo=0;
              for(int i=0; i< New.GetLength(); i++ )
              {
              if(isalpha(New.GetAt(i)))
              {
              blo |=0x1000;
              if(isupper(New.GetAt(i))) //Upper case KeyStroke
              blo |= 0x100;
              else
              blo |=0x10; //Lower Case KeyStroke
              }
              }

              if i try it with Blood what will give me is 4352 or what i need is 1100 (1000 for being an alpha and 100 for being uppercase¨.

              "The Ultimate Limit Is Only Your Imagination."

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              Blood_HaZaRd wrote:

              if i try it with Blood what will give me is 4352 or what i need is 1100...

              You are confusing base-10 with base-16.

              "One man's wage rise is another man's price increase." - Harold Wilson

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              "Man who follows car will be exhausted." - Confucius

              1 Reply Last reply
              0
              • S Schehaider_Aymen

                int blo=0;
                for(int i=0; i< New.GetLength(); i++ )
                {
                if(isalpha(New.GetAt(i)))
                {
                blo |=0x1000;
                if(isupper(New.GetAt(i))) //Upper case KeyStroke
                blo |= 0x100;
                else
                blo |=0x10; //Lower Case KeyStroke
                }
                }

                if i try it with Blood what will give me is 4352 or what i need is 1100 (1000 for being an alpha and 100 for being uppercase¨.

                "The Ultimate Limit Is Only Your Imagination."

                A Offline
                A Offline
                Aescleal
                wrote on last edited by
                #7

                So are you trying to get a count of the number of alpha characters, the number of uppercase and the number of lowercase and encode them into a set of decimal digits?

                S 1 Reply Last reply
                0
                • A Aescleal

                  So are you trying to get a count of the number of alpha characters, the number of uppercase and the number of lowercase and encode them into a set of decimal digits?

                  S Offline
                  S Offline
                  Schehaider_Aymen
                  wrote on last edited by
                  #8

                  i dont need to count them but what i need is if the user typed all uppercases the res will be 1000. if he typed only locases it ll be 100 if he typed both it will 1100. i need sth like this coz i have more checking to do for numbers, dots. so let's say if he typed one upper, 5 low, 3 digits and zero dot the result ll be 1101

                  "The Ultimate Limit Is Only Your Imagination."

                  N A 2 Replies Last reply
                  0
                  • S Schehaider_Aymen

                    i dont need to count them but what i need is if the user typed all uppercases the res will be 1000. if he typed only locases it ll be 100 if he typed both it will 1100. i need sth like this coz i have more checking to do for numbers, dots. so let's say if he typed one upper, 5 low, 3 digits and zero dot the result ll be 1101

                    "The Ultimate Limit Is Only Your Imagination."

                    N Offline
                    N Offline
                    Niklas L
                    wrote on last edited by
                    #9

                    // All powers of 2 are good
                    const DWORD TypeUpper = 0x00001000;
                    const DWORD TypeLower = 0x00000100;
                    const DWORD TypeDot = 0x00000001;
                    ...

                    DWORD characterTypes = 0;

                    loop
                    {
                    if (isupper(...))
                    characterTypes |= TypeUpper;
                    else if (isdot(...))
                    characterTypes |= TypeDot;
                    else if (...)
                    ...
                    }

                    if (characterTypes == TypeUpper)
                    cout << "contains only uppercase letters\n";
                    else if (characterTypes & TypeUpper)
                    cout << "contains at least one uppercase letter\n";

                    if (characterTypes == TypeLower)
                    cout << "contains only lowercase letters\n";
                    else if (characterTypes & TypeLower)
                    cout << "contains at least one lowercase letter\n";
                    ...

                    cout.flush();

                    home

                    1 Reply Last reply
                    0
                    • S Schehaider_Aymen

                      i dont need to count them but what i need is if the user typed all uppercases the res will be 1000. if he typed only locases it ll be 100 if he typed both it will 1100. i need sth like this coz i have more checking to do for numbers, dots. so let's say if he typed one upper, 5 low, 3 digits and zero dot the result ll be 1101

                      "The Ultimate Limit Is Only Your Imagination."

                      A Offline
                      A Offline
                      Aescleal
                      wrote on last edited by
                      #10

                      In that case I'd use a union of bit fields and an integer. Then you can set bits to your hearts content and read it as a number, something like:

                      union
                      {
                      struct
                      {
                      unsigned has_upper_case_ : 1;
                      unsigned has_lower_case_ : 1;
                      unsigned has_a_digit_ : 1;
                      unsigned has_dot_ : 1;
                      } bits_;

                      unsigned as\_number\_;
                      

                      }
                      text_flags;

                      Then you can use it like:

                      text_flags.as_number_ = 0;

                      text_flags.bits_.has_a_digit_ = 1;

                      std::cout << text_flags.as_number_ << std::endl;

                      That'll print 2 on any compiler that arranges the bit fields from lowest significance to highest. I can't remember if the standard says anything about the ordering of bits - it's not the sort of thing I play with that often - so check that the ordering is what you'd expect. IF you do use something like this you can add a constructor and members to your union. That might be an idea to save fannying about remembering to zero the thing and you could have set and reset methods. Cheers, Ash PS: There's a good description of bit fields in "The C++ Programming Language" 3rd edition if you get stuck and want help from the great man himself. I think he uses the page tables of a RISC processor as somewhere you could use bit fields like this. PPS: Corrected a typo in the code, bits in the declaration of the union didn't have the trailing underscore I gave it in use.

                      modified on Thursday, June 10, 2010 4:02 AM

                      S 1 Reply Last reply
                      0
                      • A Aescleal

                        In that case I'd use a union of bit fields and an integer. Then you can set bits to your hearts content and read it as a number, something like:

                        union
                        {
                        struct
                        {
                        unsigned has_upper_case_ : 1;
                        unsigned has_lower_case_ : 1;
                        unsigned has_a_digit_ : 1;
                        unsigned has_dot_ : 1;
                        } bits_;

                        unsigned as\_number\_;
                        

                        }
                        text_flags;

                        Then you can use it like:

                        text_flags.as_number_ = 0;

                        text_flags.bits_.has_a_digit_ = 1;

                        std::cout << text_flags.as_number_ << std::endl;

                        That'll print 2 on any compiler that arranges the bit fields from lowest significance to highest. I can't remember if the standard says anything about the ordering of bits - it's not the sort of thing I play with that often - so check that the ordering is what you'd expect. IF you do use something like this you can add a constructor and members to your union. That might be an idea to save fannying about remembering to zero the thing and you could have set and reset methods. Cheers, Ash PS: There's a good description of bit fields in "The C++ Programming Language" 3rd edition if you get stuck and want help from the great man himself. I think he uses the page tables of a RISC processor as somewhere you could use bit fields like this. PPS: Corrected a typo in the code, bits in the declaration of the union didn't have the trailing underscore I gave it in use.

                        modified on Thursday, June 10, 2010 4:02 AM

                        S Offline
                        S Offline
                        Schehaider_Aymen
                        wrote on last edited by
                        #11

                        Thank you soo much the first one runs great the secondm francly i didn't undestand it so i ll try it later :laugh: anyway thank you again for your great help :thumbsup:

                        "The Ultimate Limit Is Only Your Imagination."

                        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