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. bit extraction from BYTE

bit extraction from BYTE

Scheduled Pinned Locked Moved C / C++ / MFC
c++
10 Posts 7 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.
  • H Offline
    H Offline
    hrishi321
    wrote on last edited by
    #1

    HI i am using VC++ 2005 I have a BYTE of data, which is of 8 bits 76543210 .....this BYTE consist of 3 fields in it, which are as follows... field 1.... bit 0-3 field 2.... bit 4 field 3.... bit 5 6 7 Now I want to extract all these 3 fields and store in 3 differernt variable.... please provide me an efficient way to do it... thanks in advance

    C N _ C K 5 Replies Last reply
    0
    • H hrishi321

      HI i am using VC++ 2005 I have a BYTE of data, which is of 8 bits 76543210 .....this BYTE consist of 3 fields in it, which are as follows... field 1.... bit 0-3 field 2.... bit 4 field 3.... bit 5 6 7 Now I want to extract all these 3 fields and store in 3 differernt variable.... please provide me an efficient way to do it... thanks in advance

      C Offline
      C Offline
      Cool_Dev
      wrote on last edited by
      #2

      use logical AND (&) and bit shift operations (<< , >>) in efficient ways..

      K 1 Reply Last reply
      0
      • H hrishi321

        HI i am using VC++ 2005 I have a BYTE of data, which is of 8 bits 76543210 .....this BYTE consist of 3 fields in it, which are as follows... field 1.... bit 0-3 field 2.... bit 4 field 3.... bit 5 6 7 Now I want to extract all these 3 fields and store in 3 differernt variable.... please provide me an efficient way to do it... thanks in advance

        N Offline
        N Offline
        normanS
        wrote on last edited by
        #3

        Read up on unions and structures - that's the elegant way to do it. Bitwise-and and right-shifting (suggested by the previous poster) will also do the job nicely.

        1 Reply Last reply
        0
        • H hrishi321

          HI i am using VC++ 2005 I have a BYTE of data, which is of 8 bits 76543210 .....this BYTE consist of 3 fields in it, which are as follows... field 1.... bit 0-3 field 2.... bit 4 field 3.... bit 5 6 7 Now I want to extract all these 3 fields and store in 3 differernt variable.... please provide me an efficient way to do it... thanks in advance

          _ Offline
          _ Offline
          _Superman_
          wrote on last edited by
          #4

          field1 = (data & 0x0F);
          field2 = (data & 0x10) >> 4;
          field3 = (data & 0xE0) >> 5;

          «_Superman_» I love work. It gives me something to do between weekends.
          Microsoft MVP (Visual C++)

          M 1 Reply Last reply
          0
          • _ _Superman_

            field1 = (data & 0x0F);
            field2 = (data & 0x10) >> 4;
            field3 = (data & 0xE0) >> 5;

            «_Superman_» I love work. It gives me something to do between weekends.
            Microsoft MVP (Visual C++)

            M Offline
            M Offline
            Mohan Ramachandra
            wrote on last edited by
            #5

            A bit more optimization in field3

            «_Superman_» wrote:

            field3 = (data & 0xE0) >> 5;

            field3 = data>> 5;

            _ 1 Reply Last reply
            0
            • H hrishi321

              HI i am using VC++ 2005 I have a BYTE of data, which is of 8 bits 76543210 .....this BYTE consist of 3 fields in it, which are as follows... field 1.... bit 0-3 field 2.... bit 4 field 3.... bit 5 6 7 Now I want to extract all these 3 fields and store in 3 differernt variable.... please provide me an efficient way to do it... thanks in advance

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #6

              The two different ways (you may find they are translated to the same asm, by the compiler). [update] the first method's asm (SHIFT and AND operator) looks slightly faster)[/update]

              struct BitField
              {
              unsigned int f1:4;
              unsigned int f2:1;
              unsigned int f3:3;
              };

              union MyData
              {
              unsigned char data;
              BitField bf;
              };

              int main(int argc, char *argv[])
              {
              unsigned char data= rand();

              int field1, field2, field3;

              // using SHIFT and AND operators
              field1 = data & 0xF;
              field2 = (data >> 4) & 1;
              field3 = data >> 5;

              printf("data=%d\n", data);
              printf("(1) fields: %d, %d, %d\n", field1, field2, field3);

              // using union and bitfield struct
              MyData md;
              md.data = data;

              field1 = md.bf.f1;
              field2 = md.bf.f2;
              field3 = md.bf.f3;
              printf("(2) fields: %d, %d, %d\n", field1, field2, field3);
              return 0;
              }

              :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              modified on Wednesday, March 10, 2010 5:31 AM

              1 Reply Last reply
              0
              • M Mohan Ramachandra

                A bit more optimization in field3

                «_Superman_» wrote:

                field3 = (data & 0xE0) >> 5;

                field3 = data>> 5;

                _ Offline
                _ Offline
                _Superman_
                wrote on last edited by
                #7

                Perfect. :thumbsup:

                «_Superman_» I love work. It gives me something to do between weekends.
                Microsoft MVP (Visual C++)

                1 Reply Last reply
                0
                • H hrishi321

                  HI i am using VC++ 2005 I have a BYTE of data, which is of 8 bits 76543210 .....this BYTE consist of 3 fields in it, which are as follows... field 1.... bit 0-3 field 2.... bit 4 field 3.... bit 5 6 7 Now I want to extract all these 3 fields and store in 3 differernt variable.... please provide me an efficient way to do it... thanks in advance

                  K Offline
                  K Offline
                  KarstenK
                  wrote on last edited by
                  #8

                  field 1.... bit 0-3 = > (data & 3) and so on

                  Press F1 for help or google it. Greetings from Germany

                  C 1 Reply Last reply
                  0
                  • K KarstenK

                    field 1.... bit 0-3 = > (data & 3) and so on

                    Press F1 for help or google it. Greetings from Germany

                    C Offline
                    C Offline
                    CPallini
                    wrote on last edited by
                    #9

                    KarstenK wrote:

                    field 1.... bit 0-3 = > (data & 3)

                    field 1.... bit 0-3 = > (data & 15) FFY. :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    1 Reply Last reply
                    0
                    • C Cool_Dev

                      use logical AND (&) and bit shift operations (<< , >>) in efficient ways..

                      K Offline
                      K Offline
                      KarstenK
                      wrote on last edited by
                      #10

                      "use XYZ in a efficient way" :-O :-O :-O

                      Press F1 for help or google it. Greetings from Germany

                      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