Arbitrary Type Definition

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Now With More Salt

    Arbitrary Type Definition

    Suppose I wanted to go about defining a three or four bit unsigned integer
    type. How would I go about doing that? Thanks


  • Buster

    #2
    Re: Arbitrary Type Definition

    "Now With More Salt" <coloredlikefru it@yahoo.com> wrote[color=blue]
    > Suppose I wanted to go about defining a three or four bit unsigned integer
    > type. How would I go about doing that? Thanks[/color]

    You're going to have to provide a bit more motivation and explanation.
    C++ doesn't have a 16 bit integral type, a 32 bit integral type or an 8 bit
    integral type and gets along fine without them. What are you trying to do?

    Regards,
    Buster.


    Comment

    • David B. Held

      #3
      Re: Arbitrary Type Definition

      "Now With More Salt" <coloredlikefru it@yahoo.com> wrote in message
      news:bka7te$aj8 $1@news-int.gatech.edu. ..[color=blue]
      > Suppose I wanted to go about defining a three or four bit
      > unsigned integer type. How would I go about doing that?[/color]

      You could use a bit field embedded in a struct, but it would
      not be treated as an equal with the builtins and would not
      be packed optimally. C++ doesn't really let you specify the
      size of types in that amount of detail. The resolution for type
      sizes is sizeof(char). However, if you specify the operations
      you want to support, and the expectations you have for the
      type, you might be able to cobble together something that is
      suitable for your purposes.

      Dave


      Comment

      • Mike Wahler

        #4
        Re: Arbitrary Type Definition


        Now With More Salt <coloredlikefru it@yahoo.com> wrote in message
        news:bka7te$aj8 $1@news-int.gatech.edu. ..[color=blue]
        > Suppose I wanted to go about defining a three or four bit unsigned integer
        > type. How would I go about doing that?[/color]

        I'd create a class that wraps a std::bitset<num _of_bits>
        and define whatever operations I need (e.g. arithmetic,
        comparison with other types, etc.)

        -Mike



        Comment

        • Thomas Matthews

          #5
          Re: Arbitrary Type Definition

          Now With More Salt wrote:[color=blue]
          > Suppose I wanted to go about defining a three or four bit unsigned integer
          > type. How would I go about doing that? Thanks
          >
          >[/color]

          One method is to use an existing integral type that is bigger
          than what you need, and perhaps typedef it.

          For example, if your platform's native integral type uses
          64 bits, then you would only use the "lower" 3 or 4 bits.

          But if your processor has a native integral type that
          uses 3 or 4 bits, then I suggest assembly language.

          --
          Thomas Matthews

          C++ newsgroup welcome message:

          C++ Faq: http://www.parashift.com/c++-faq-lite
          C Faq: http://www.eskimo.com/~scs/c-faq/top.html
          alt.comp.lang.l earn.c-c++ faq:

          Other sites:
          http://www.josuttis.com -- C++ STL Library book

          Comment

          Working...