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 field question

bit field question

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

    I don't use bit fields much at all. I have a simple struct as folows (on an INTEL format machine): typedef struct{ short f1 : 13; short f2 : 1; short f3 : 1; short f4 : 1; } tststruct; tststruct tst; tst.f1 = 8000; what ends up happening is f1 gets a value of 0xFF40 instead of what I thought I would get (0x1F40). The other members (f2,f3,f4) are all 0x00 as I would expect. Any idea why?

    S 1 Reply Last reply
    0
    • L lynchspawn

      I don't use bit fields much at all. I have a simple struct as folows (on an INTEL format machine): typedef struct{ short f1 : 13; short f2 : 1; short f3 : 1; short f4 : 1; } tststruct; tststruct tst; tst.f1 = 8000; what ends up happening is f1 gets a value of 0xFF40 instead of what I thought I would get (0x1F40). The other members (f2,f3,f4) are all 0x00 as I would expect. Any idea why?

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      Maybe the compiler fills up the remaining 3 bits with ones instead of zeros? Maybe you can do an & with 0x1FFF to filter out the first 3 bits. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

      L 1 Reply Last reply
      0
      • S S Senthil Kumar

        Maybe the compiler fills up the remaining 3 bits with ones instead of zeros? Maybe you can do an & with 0x1FFF to filter out the first 3 bits. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

        L Offline
        L Offline
        lynchspawn
        wrote on last edited by
        #3

        Thanks. Here's what I found: if I use unsigned shorts, everything works fine. Apparently, the value is sign- extended when using signed variables - so the 1F40 (when looking at it as a 13 bit number) is a negative number and the sign bit is extended out when placing the value in the struct (giving FF40). Hope that makes sense. Thanks - I got there eventually but it really looked strange at first.

        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