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. Algorithms
  4. contiguous bits algorithm.

contiguous bits algorithm.

Scheduled Pinned Locked Moved Algorithms
algorithmstutorial
13 Posts 7 Posters 9 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.
  • C cp9876

    There might be some nifty bit processing, but this should work with no more than about 6 comparisons (32 bits - 62 options) 8-bit version:

    int fn(int x)
       {
       switch (x)
          case 0x01:
          case 0x03:
          case 0x07:
          case 0x0F:
          case 0x1F:
          case 0x3F:
          case 0x7F:
             return 1;
          case 0x80:
          case 0xC0:
          case 0xE0:
          case 0xF0:
          case 0xF8:
          case 0xFC:
          case 0xFE:
             return 1;
          default:
             return 0;
       }
    

    I think compilers are quite good at optimizing this sort of code, but I have never tested it so I'd be interested in comments.


    Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."

    C Offline
    C Offline
    chandu004
    wrote on last edited by
    #4

    thanks, the above logic is simple and works as of now. but the ultimate goal is to design a function in the following prototype. int GetWordStatus(int word,int word length) { //code here } if the word length is certainly 8 or 16 or 32, then teh above logic is appropriate. but the checking has to be done on the first specified number of bits. i.e. wordlength. hope i dont need to give any example, if required, i can. thanku.

    C R 2 Replies Last reply
    0
    • C chandu004

      thanks, the above logic is simple and works as of now. but the ultimate goal is to design a function in the following prototype. int GetWordStatus(int word,int word length) { //code here } if the word length is certainly 8 or 16 or 32, then teh above logic is appropriate. but the checking has to be done on the first specified number of bits. i.e. wordlength. hope i dont need to give any example, if required, i can. thanku.

      C Offline
      C Offline
      cp9876
      wrote on last edited by
      #5

      You should be able to extend this - do something like:

      const int hi1 = {0xFF, 0xFE, 0xFC, 0xF8, 0xF0, 0xE0, 0xC0, 0x80, 0};
      
      int GetWordStatus(int word, int wordlength)
         {
         ASSERT(wordLength <= 8  &&  wordLength >= 0);
         int x = word & ~hi1[wordLength]; // may not be necessary depending on what data can be sent
         switch (x)
            {
            case 0x01:
            case 0x03:
            case 0x07:
            case 0x0F:
            case 0x1F:
            case 0x3F:
            case 0x7F:
               return 1;
            }
         x = word | hi1[wordLength];
         switch (x)
            {
            case 0x80:
            case 0xC0:
            case 0xE0:
            case 0xF0:
            case 0xF8:
            case 0xFC:
            case 0xFE:
               return 2;
            }
         return 0;
         }
      

      It may be a bit inefficient for small wordlengths, if it is a problem you should spend some time and make it better. -- modified at 6:57 Monday 3rd September, 2007 The reason I am suggesting a switch statement rather than a loop is that I thought the reason that in C/C++ the case values had to be constants was so that the compiler could order them and implement the switch as a simple binary search, so for N case statements there would be only log2(N) comparisons. I can't easily find a reference for this now - does anyone know if this happens?


      Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."

      R 1 Reply Last reply
      0
      • C chandu004

        thanks, the above logic is simple and works as of now. but the ultimate goal is to design a function in the following prototype. int GetWordStatus(int word,int word length) { //code here } if the word length is certainly 8 or 16 or 32, then teh above logic is appropriate. but the checking has to be done on the first specified number of bits. i.e. wordlength. hope i dont need to give any example, if required, i can. thanku.

        R Offline
        R Offline
        Russell
        wrote on last edited by
        #6

        In this case I think that the better solution is a general solution...so a loop like

        tmpword=word;
        for(i=0;i<wordlength;i++){
        if(tmpword == 1) return 1;
        if(tmpword & 1 ) tmpword >>=1;
        else break;
        }

        tmpword=word;
        for(i=0;i<wordlength;i++){
        if(tmpword == 0x80) return 2;
        if(tmpword & 0x80 ) tmpword <<=1;
        else break;
        }

        return 0;

        or somethig similar to this (I haven't tested the code...it is only an idea of what I'm meaning above...:~ )


        Russell

        1 Reply Last reply
        0
        • C cp9876

          You should be able to extend this - do something like:

          const int hi1 = {0xFF, 0xFE, 0xFC, 0xF8, 0xF0, 0xE0, 0xC0, 0x80, 0};
          
          int GetWordStatus(int word, int wordlength)
             {
             ASSERT(wordLength <= 8  &&  wordLength >= 0);
             int x = word & ~hi1[wordLength]; // may not be necessary depending on what data can be sent
             switch (x)
                {
                case 0x01:
                case 0x03:
                case 0x07:
                case 0x0F:
                case 0x1F:
                case 0x3F:
                case 0x7F:
                   return 1;
                }
             x = word | hi1[wordLength];
             switch (x)
                {
                case 0x80:
                case 0xC0:
                case 0xE0:
                case 0xF0:
                case 0xF8:
                case 0xFC:
                case 0xFE:
                   return 2;
                }
             return 0;
             }
          

          It may be a bit inefficient for small wordlengths, if it is a problem you should spend some time and make it better. -- modified at 6:57 Monday 3rd September, 2007 The reason I am suggesting a switch statement rather than a loop is that I thought the reason that in C/C++ the case values had to be constants was so that the compiler could order them and implement the switch as a simple binary search, so for N case statements there would be only log2(N) comparisons. I can't easily find a reference for this now - does anyone know if this happens?


          Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."

          R Offline
          R Offline
          Russell
          wrote on last edited by
          #7

          cp9876 wrote:

          I can't easily find a reference for this now - does anyone know if this happens?

          Try to look the disassembly window opening it to the code you suggested ... :) I think that this shortcut is applyed from the compiler only if the number of case is greather then a specified number ... and only if you turn on that optimizations that is probally related with faster applications, but not applyed in small exe file dimension.


          Russell

          1 Reply Last reply
          0
          • C chandu004

            i have a 16 bit variable(unsigned short). now i want a best algorithm (function) which will do the following. 1.if contiguous bits at LSB are high then the function should return 1. for example, consider the following bit patterns 0000000001111111 or 0000111111111111 or 0000000000000001 2.if continuous bits at MSB are high then the function should return 2. for example, 1110000000000000 and so on. 3.other wise it should return 0 for the examoples such as 1010000000000011 0000011101010011 and so on thanks in advance. any more clarifications required, please get back.

            R Offline
            R Offline
            rihdus
            wrote on last edited by
            #8

            try this vb code: Private Function GetWordStatus(ByVal word As Integer) As Integer Dim significantLength As Integer = (word & "").Length 'Calculate significant Length of number Dim temp As Integer temp = (2 ^ significantLength) - 1 If Convert.ToInt64(word, 2) = temp Then Return 1 Else Dim cntr As Integer temp = 2 ^ (significantLength - 1) cntr = (significantLength - 1) While cntr > 1 If Convert.ToInt64(word, 2) = temp Then Return 2 Else temp += 2 ^ (cntr - 1) End If cntr = cntr - 1 End While End If ' Return 0 End Function Its not the best but an alternative :)

            Any systematic work reflects its significance for a long time. So let's discuss the best...

            1 Reply Last reply
            0
            • C chandu004

              i have a 16 bit variable(unsigned short). now i want a best algorithm (function) which will do the following. 1.if contiguous bits at LSB are high then the function should return 1. for example, consider the following bit patterns 0000000001111111 or 0000111111111111 or 0000000000000001 2.if continuous bits at MSB are high then the function should return 2. for example, 1110000000000000 and so on. 3.other wise it should return 0 for the examoples such as 1010000000000011 0000011101010011 and so on thanks in advance. any more clarifications required, please get back.

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #9

              Hi, it is much simpler than that. one spec is missing: what if input is zero ? THEORY (assume unsigned integers) 1. if all zeroes followed by all ones, then one more becomes a power of two 2. if all ones followed by all zeroes, then bitwise complement reverts to first case 3. a power of two has just one bit set; it can be tested in one line:

              if (( x & (-x) )==x) then either x = zero or x = power of 2 !!!

              CODE

              if (x==0) return whatever you think is appropriate
              if ((x&(-x))==x) return 1;
              x=~x;
              if ((x&(-x)==x) return 2;
              return 3;

              :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


              R C 2 Replies Last reply
              0
              • L Luc Pattyn

                Hi, it is much simpler than that. one spec is missing: what if input is zero ? THEORY (assume unsigned integers) 1. if all zeroes followed by all ones, then one more becomes a power of two 2. if all ones followed by all zeroes, then bitwise complement reverts to first case 3. a power of two has just one bit set; it can be tested in one line:

                if (( x & (-x) )==x) then either x = zero or x = power of 2 !!!

                CODE

                if (x==0) return whatever you think is appropriate
                if ((x&(-x))==x) return 1;
                x=~x;
                if ((x&(-x)==x) return 2;
                return 3;

                :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


                R Offline
                R Offline
                Russell
                wrote on last edited by
                #10

                You won:cool:


                Russell

                1 Reply Last reply
                0
                • L Luc Pattyn

                  Hi, it is much simpler than that. one spec is missing: what if input is zero ? THEORY (assume unsigned integers) 1. if all zeroes followed by all ones, then one more becomes a power of two 2. if all ones followed by all zeroes, then bitwise complement reverts to first case 3. a power of two has just one bit set; it can be tested in one line:

                  if (( x & (-x) )==x) then either x = zero or x = power of 2 !!!

                  CODE

                  if (x==0) return whatever you think is appropriate
                  if ((x&(-x))==x) return 1;
                  x=~x;
                  if ((x&(-x)==x) return 2;
                  return 3;

                  :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


                  C Offline
                  C Offline
                  cp9876
                  wrote on last edited by
                  #11

                  I knew there would be a clever bit twiddling answer to this - very neat!


                  Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."

                  1 Reply Last reply
                  0
                  • C chandu004

                    i have a 16 bit variable(unsigned short). now i want a best algorithm (function) which will do the following. 1.if contiguous bits at LSB are high then the function should return 1. for example, consider the following bit patterns 0000000001111111 or 0000111111111111 or 0000000000000001 2.if continuous bits at MSB are high then the function should return 2. for example, 1110000000000000 and so on. 3.other wise it should return 0 for the examoples such as 1010000000000011 0000011101010011 and so on thanks in advance. any more clarifications required, please get back.

                    P Offline
                    P Offline
                    polyhedron
                    wrote on last edited by
                    #12

                    fn(WORD w) { WORD mark=0x8000; if(w&mark) { mark >>= 1; while( (w&mark) && mark ) mark >>= 1; if(mark==0)//w must = 0xffff,return 1 or 2 return 2;//also can return 1 if(w&(mark-1)) return 0; else return 2; } else { mark >>= 1; while( ((w&mark)==0) && mark ) mark >>= 1; if( (w&(mark-1))==(mark-1) ) return 1; else return 0; } }

                    1 Reply Last reply
                    0
                    • C chandu004

                      i have a 16 bit variable(unsigned short). now i want a best algorithm (function) which will do the following. 1.if contiguous bits at LSB are high then the function should return 1. for example, consider the following bit patterns 0000000001111111 or 0000111111111111 or 0000000000000001 2.if continuous bits at MSB are high then the function should return 2. for example, 1110000000000000 and so on. 3.other wise it should return 0 for the examoples such as 1010000000000011 0000011101010011 and so on thanks in advance. any more clarifications required, please get back.

                      N Offline
                      N Offline
                      Nelek
                      wrote on last edited by
                      #13

                      Hi, what about...

                      int GetWordStatus(int word,int word length)
                      {
                      if (word == 0)
                      return 0;

                      int nHigh = word / pow (2, word_length);
                      int nLow = word % pow (2, word_length);

                      if (nHigh > nLow)
                      return 1;

                      else if (nHigh < nLow)
                      return 2;

                      else
                      return 3;
                      }

                      Greetings. -------- M.D.V. If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you ;)

                      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