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. Bits;

Bits;

Scheduled Pinned Locked Moved C / C++ / MFC
question
4 Posts 3 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.
  • B Offline
    B Offline
    Bo Hunter
    wrote on last edited by
    #1

    I have used a bit mask before but how does one extract individual bits. Say I wanted to extract bit 28 from 32 bit int, how do I do that? Thank You Bo Hunter

    N H 2 Replies Last reply
    0
    • B Bo Hunter

      I have used a bit mask before but how does one extract individual bits. Say I wanted to extract bit 28 from 32 bit int, how do I do that? Thank You Bo Hunter

      N Offline
      N Offline
      Neville Franks
      wrote on last edited by
      #2

      Try this: int nvalue = 0xabcd; if ( nvalue & (1<<28) ) ; // bit 28 is set Neville Franks, Author of ED for Windows www.getsoft.com and Surfulater www.surfulater.com "Save what you Surf"

      B 1 Reply Last reply
      0
      • N Neville Franks

        Try this: int nvalue = 0xabcd; if ( nvalue & (1<<28) ) ; // bit 28 is set Neville Franks, Author of ED for Windows www.getsoft.com and Surfulater www.surfulater.com "Save what you Surf"

        B Offline
        B Offline
        Bo Hunter
        wrote on last edited by
        #3

        That is exactly what I needed. Thank You Bo Hunter

        1 Reply Last reply
        0
        • B Bo Hunter

          I have used a bit mask before but how does one extract individual bits. Say I wanted to extract bit 28 from 32 bit int, how do I do that? Thank You Bo Hunter

          H Offline
          H Offline
          Henry miller
          wrote on last edited by
          #4

          The other guys code works, and sometimes it is best. However consider the following #define FOO_MASK 0x01000000 //0000 0001 0000 0000 0000 0000 0000 0000 0000, mask for foo ... if(my_var && FOO_MASK) { .... This has the advantage that when I read your code I know instantly you are looking for foo, while the other guys code just gives me (1 >> 28). It takes a moment to realize that he is looking at bit 29, and still gives me no indication of what bit 29 is! Instead of the above you can also use bitfields: #pragma pack(1) struct myStruct { long foo :28; long bar :1; long baz :3; } #pragma pack() ... if(mystrct.bar) { ... This is compiler and CPU specific, but it works fairly often in the real world, and is more readable. If you must use the other guys code, please do it in a class, and only in one place. Then comment what is going on so when I come across it 10 years from now I can safely make a change.

          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