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. 4 bit datatype?

4 bit datatype?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
3 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.
  • _ Offline
    _ Offline
    _NielsB
    wrote on last edited by
    #1

    Hi, I need to define a datatype with a size of 4 bit within a struct. char has a size of 8 bit and thats the smallest type I know ... So what else should I do? I really dont know how to search for this, sorry. btw: I think I saw something like "unsigned short varName:4" in a code-snipet. Does this mean the varName holds 4bit of data? Thanks in advance!

    D 1 Reply Last reply
    0
    • _ _NielsB

      Hi, I need to define a datatype with a size of 4 bit within a struct. char has a size of 8 bit and thats the smallest type I know ... So what else should I do? I really dont know how to search for this, sorry. btw: I think I saw something like "unsigned short varName:4" in a code-snipet. Does this mean the varName holds 4bit of data? Thanks in advance!

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      _NielsB wrote:

      So what else should I do?

      Something like:

      struct
      {
      char c:4;
      };


      "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      B 1 Reply Last reply
      0
      • D David Crow

        _NielsB wrote:

        So what else should I do?

        Something like:

        struct
        {
        char c:4;
        };


        "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

        B Offline
        B Offline
        Bram van Kampen
        wrote on last edited by
        #3

        DavidCrow wrote:

        Something like: struct{ char c:4;};

        This is indeed a respected method inkeeping with the language. However it also depends on what the four bit data type is to be used for, Is packing on a Four Bit boundary required.If a lot of manipulating code is to be written, or an array of nibbles needs to be addressed, maybe a more transparent way is Masking and Shifting, and a Raft of Macro's to cover it. e.g.: #define LO_NIBBLE(x) ((x)&0xFF) #define HI_NIBBLE(x) (((x)>4))&0xFF) #define GET_ARR_ITEM(Arr,N) ((((N)&1)==0)?LO_NIBBLE((Arr)[(N)/2]:HI_NIBBLE((Arr)[(N)/2]) And so on ad infinitum Even further, depending on the importance and complexity of the grander problem to be solved, (and the Time and resources available)one could conceive of a new class, CNibble with an entire gammet of overloaded operators. Come to think of it, why not also a Class CHalfNibble and CBit. Hope this is of help, :) regards

        LateNightsInNewry

        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