Defining a 32 Bit Integer on every platform?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Frank Tombe

    Defining a 32 Bit Integer on every platform?

    Hi,

    i want to write a chat program based on a self writen network
    protocol. It should run on any platform so there is one problem: On
    one platform an Integer 32 Bit on other 64. But i need exact a 32 Bit
    one (for compatibly issues). It should be also possible to add and
    subtract etc from this type... So how can i define this type?


    Thx Frank
  • Gianni Mariani

    #2
    Re: Defining a 32 Bit Integer on every platform?

    Frank Tombe wrote:[color=blue]
    > Hi,
    >
    > i want to write a chat program based on a self writen network
    > protocol. It should run on any platform so there is one problem: On
    > one platform an Integer 32 Bit on other 64. But i need exact a 32 Bit
    > one (for compatibly issues). It should be also possible to add and
    > subtract etc from this type... So how can i define this type?
    >
    >
    > Thx Frank[/color]

    What about byte order ?


    Anyhow, the usual way to solve this is to use a typedef that is defined
    for each platform.

    There are some tricks to make the byte-ordering also transparent, if
    you're interested, just ask.

    Comment

    • David Harmon

      #3
      Re: Defining a 32 Bit Integer on every platform?

      On 8 May 2004 08:29:22 -0700 in comp.lang.c++, ncoretex@freequ ote.net
      (Frank Tombe) wrote,[color=blue]
      >i want to write a chat program based on a self writen network
      >protocol. It should run on any platform so there is one problem: On
      >one platform an Integer 32 Bit on other 64. But i need exact a 32 Bit
      >one (for compatibly issues). It should be also possible to add and
      >subtract etc from this type... So how can i define this type?[/color]

      Does your 64bit platform offer any 32bit integer datatype at all?
      No reason why it has to, but perhaps that platform is especially
      unsuited to your requirements and should not be used.

      Out of curiosity, what is it?

      Compare <boost/cstdint.hpp> from http://www.boost.org

      Comment

      • JKop

        #4
        Re: Defining a 32 Bit Integer on every platform?

        Gianni Mariani posted:
        [color=blue]
        > Frank Tombe wrote:[color=green]
        >> Hi,
        >>
        >> i want to write a chat program based on a self writen network
        >> protocol. It should run on any platform so there is one problem: On
        >> one platform an Integer 32 Bit on other 64. But i need exact a 32 Bit
        >> one (for compatibly issues). It should be also possible to add and
        >> subtract etc from this type... So how can i define this type?
        >>
        >>
        >> Thx Frank[/color]
        >
        > What about byte order ?
        >
        >
        > Anyhow, the usual way to solve this is to use a typedef that is defined
        > for each platform.
        >
        > There are some tricks to make the byte-ordering also transparent, if
        > you're interested, just ask.
        >[/color]


        I myself have done the following to get out of the byte-order problemo:


        union Numbr
        {
        struct {
        unsigned __int8 a;
        unsigned __int8 b;
        unsigned __int8 c;
        unsigned __int8 d; }

        unsigned __int32 ThirtyTwoBit;
        };

        Comment

        • Gianni Mariani

          #5
          Re: Defining a 32 Bit Integer on every platform?

          JKop wrote:[color=blue]
          > Gianni Mariani posted:
          >
          >[/color]
          ....[color=blue][color=green]
          >>
          >>There are some tricks to make the byte-ordering also transparent, if
          >>you're interested, just ask.
          >>[/color]
          >
          >
          >
          > I myself have done the following to get out of the byte-order problemo:
          >
          >
          > union Numbr
          > {
          > struct {
          > unsigned __int8 a;
          > unsigned __int8 b;
          > unsigned __int8 c;
          > unsigned __int8 d; }
          >
          > unsigned __int32 ThirtyTwoBit;
          > };[/color]

          I posted an answer a while ago - here is the google-groups cache of the
          post.





          Comment

          • Ian

            #6
            Re: Defining a 32 Bit Integer on every platform?

            JKop wrote:[color=blue]
            > Gianni Mariani posted:
            >
            >[color=green]
            >>Frank Tombe wrote:
            >>[color=darkred]
            >>>Hi,
            >>>
            >>>i want to write a chat program based on a self writen network
            >>>protocol. It should run on any platform so there is one problem: On
            >>>one platform an Integer 32 Bit on other 64. But i need exact a 32 Bit
            >>>one (for compatibly issues). It should be also possible to add and
            >>>subtract etc from this type... So how can i define this type?
            >>>
            >>>
            >>>Thx Frank[/color]
            >>
            >>What about byte order ?
            >>
            >>
            >>Anyhow, the usual way to solve this is to use a typedef that is defined
            >>for each platform.
            >>
            >>There are some tricks to make the byte-ordering also transparent, if
            >>you're interested, just ask.
            >>[/color]
            >
            >
            >
            > I myself have done the following to get out of the byte-order problemo:
            >
            >
            > union Numbr
            > {
            > struct {
            > unsigned __int8 a;
            > unsigned __int8 b;
            > unsigned __int8 c;
            > unsigned __int8 d; }
            >
            > unsigned __int32 ThirtyTwoBit;
            > };[/color]
            Whats wrong with using uint32_t and htonl/ntohl and friends?

            Ian

            Comment

            • Gianni Mariani

              #7
              Re: Defining a 32 Bit Integer on every platform?

              Ian wrote:[color=blue]
              > JKop wrote:
              >[/color]
              ....[color=blue]
              >
              > Whats wrong with using uint32_t and htonl/ntohl and friends?[/color]

              htonl/ntohl only works with 32 or 16 bit numbers and you have to cast
              back and forth for types other than unsigned. class NetworkOrder (see
              http://tinyurl.com/2ffdw ) work with any byte order specific type and
              does the magic of applying the endian translations for you, not to
              mention that on a platform that has the right endianness, it will be
              pretty fast compared to a function call.



              Comment

              • Ian

                #8
                Re: Defining a 32 Bit Integer on every platform?

                Gianni Mariani wrote:[color=blue]
                > Ian wrote:
                >[color=green]
                >> JKop wrote:
                >>[/color]
                > ...
                >[color=green]
                >>
                >> Whats wrong with using uint32_t and htonl/ntohl and friends?[/color]
                >
                >
                > htonl/ntohl only works with 32 or 16 bit numbers and you have to cast
                > back and forth for types other than unsigned. class NetworkOrder (see
                > http://tinyurl.com/2ffdw ) work with any byte order specific type and
                > does the magic of applying the endian translations for you, not to
                > mention that on a platform that has the right endianness, it will be
                > pretty fast compared to a function call.
                >[/color]
                On a platform with the right endianness, htonl/ntohl will be #defined to
                do nothing, which is faster still.

                Never mind, I see your point.

                Ian

                Comment

                Working...