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. Other Discussions
  3. Clever Code
  4. Bit fields, get that type-spec right [modified]

Bit fields, get that type-spec right [modified]

Scheduled Pinned Locked Moved Clever Code
help
2 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.
  • C Offline
    C Offline
    cmk
    wrote on last edited by
    #1

    Just found this one:

    typedef unsigned char byte;
    typedef unsigned int uint;
    #pragma CKPRAGMA_PACK_PUSH(1)

    typedef struct SkGuidTime {
    short year; /* 16 */ // -32768 - +32,767
    byte month : 4; // 1 - 12
    byte day : 5; // 1 - 31, Day of Month
    //.................
    byte hour : 5; // 0 - 23
    byte min : 6; // 0 - 59
    byte sec : 6; // 0 - 59
    //.................
    byte res : 6; // 0 - 63
    } SkGuidTime; /* 48 */ // 6 bytes

    #pragma CKPRAGMA_PACK_POP

    This is being used as part of a function that generates a GUID. After a SkGuidTime tm is filled it is memcpy'd into the GUID: memcpy((byte*)&g.Data3, &tm, 6); It was noticed that the time section of the GUID wasn't changing across seconds, only minutes. ... unfortunately the structure, although compiles without error or warning, is actually 8 bytes. Each element gets it's own byte. Changing the type from byte to ushort or larger fixes things.

    ...cmk Save the whales - collect the whole set

    K 1 Reply Last reply
    0
    • C cmk

      Just found this one:

      typedef unsigned char byte;
      typedef unsigned int uint;
      #pragma CKPRAGMA_PACK_PUSH(1)

      typedef struct SkGuidTime {
      short year; /* 16 */ // -32768 - +32,767
      byte month : 4; // 1 - 12
      byte day : 5; // 1 - 31, Day of Month
      //.................
      byte hour : 5; // 0 - 23
      byte min : 6; // 0 - 59
      byte sec : 6; // 0 - 59
      //.................
      byte res : 6; // 0 - 63
      } SkGuidTime; /* 48 */ // 6 bytes

      #pragma CKPRAGMA_PACK_POP

      This is being used as part of a function that generates a GUID. After a SkGuidTime tm is filled it is memcpy'd into the GUID: memcpy((byte*)&g.Data3, &tm, 6); It was noticed that the time section of the GUID wasn't changing across seconds, only minutes. ... unfortunately the structure, although compiles without error or warning, is actually 8 bytes. Each element gets it's own byte. Changing the type from byte to ushort or larger fixes things.

      ...cmk Save the whales - collect the whole set

      K Offline
      K Offline
      Kochise
      wrote on last edited by
      #2

      The bit field specifiers aren't ignored if it can fit on byte's length : byte month : 4; // 1 - 12 byte day : 4; // 1 - 16 This will fits in a byte ! To be sure everything to be in your uint, always stuff things like you done with 'res' to fill everything... Kochise

      In Code we trust !

      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