How do i declare a byte variable?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Frederick Gotham

    #16
    Re: How do i declare a byte variable?

    Ian Collins posted:
    No, they are not. "unsigned" is shorthand for "unsigned int", which is
    only required to represent values up to 65535.

    We were referring to usigned integer types (note the plural).

    --

    Frederick Gotham

    Comment

    • Frederick Gotham

      #17
      Re: How do i declare a byte variable?

      Clark S. Cox III posted:
      Who said anything about "unsigned long"? The type in question was
      "unsigned" (aka "unsigned int")

      I see where the confusion stems from. I initially said that the following
      is fine:

      unsigned i = 2147483647;

      What I meant was that it is not ill-formed, nor does it invoke undefined
      behaviour. (However, it would have been more intuitive had I written:

      long unsigned i = 2147483647;

      The point I was trying to make is that the following statement is ill-
      formed:

      long unsigned i = 2147483648;

      , because a program is ill-formed if it contains an integer literal which
      is out of range. We must explicitly make the literal unsigned:

      long unsigned i = 2147483648U;

      You are correct, however, that the max range of "unsigned int" is not
      obliged to exceed 65535.

      --

      Frederick Gotham

      Comment

      • Ian Collins

        #18
        Re: How do i declare a byte variable?

        Frederick Gotham wrote:
        Ian Collins posted:
        >
        >
        >>No, they are not. "unsigned" is shorthand for "unsigned int", which is
        >>only required to represent values up to 65535.
        >
        >
        >
        We were referring to usigned integer types (note the plural).
        >
        Nonsense, you said:
        The following is guaranteed to work fine on every system:
        >
        unsigned i = 2147483647;
        Which is false and was corrected by Clark.

        You then said:
        >"unsigned long" must have at least the following range:
        >
        0 through 4294967295
        >
        Sounds like these systems you mention are not Standard-compliant.
        Singular and very specific.

        --
        Ian Collins.

        Comment

        • Frederick Gotham

          #19
          Re: How do i declare a byte variable?

          Ian Collins posted:
          >We were referring to usigned integer types (note the plural).
          >>
          Nonsense, you said:
          >
          >The following is guaranteed to work fine on every system:
          >>
          > unsigned i = 2147483647;
          >
          Which is false and was corrected by Clark.

          I explained this elsethread. The following statement is neither ill-formed,
          nor does it invoke undefine behaviour (hence fine):


          unsigned i = 2147483647;

          --

          Frederick Gotham

          Comment

          • Martijn van Buul

            #20
            Re: How do i declare a byte variable?

            It occurred to me that Frederick Gotham wrote in comp.lang.c++:
            Howard posted:
            >
            The extra efficiency of "unsigned int" is worth the cost of using up
            another three bytes (or thereabouts).
            Utter nonsense. There are plenty of reasons and plenty of applications
            where you *do* want to use a char if possible, and where it would even
            be *faster*.
            >
            Even if I were to write:
            >
            char unsigned i = 255;
            >
            , then it would still probably consume 4 bytes (or thereabouts) on most
            systems.
            *most* systems? Nonsense. Only on systems with a severe alignment problem.
            While there might be processors out there which do suffer from this, they
            are most definately NOT common.
            (If you have a system which can only access 4 bytes of memory at a
            time, rather than one sole byte, then it would be efficient to store the
            byte value in the least significant byte of the 4 bytes, and to just ignore
            the values of the subsequent 3 bytes).
            1) Those systems, should they exist, are rare.
            2) It would only be "efficient" from a minimum-execution-time point of view.
            It's grossly inefficient regarding memory usage.
            >
            An easy test for this is the following:
            >
            #include <stdio.h>
            >
            int main(void)
            {
            struct FourBytes { char a,b,c,d; };
            >
            switch(sizeof(s truct FourBytes))
            {
            case sizeof(char[4]):
            puts("Packed nice and tight!");
            break;
            This "proves" nor disproves absolutely nothing.
            Notwithstanding any of this, your char is going to be promoted to int every
            time you lay a finger on it.
            Nonsense. Again.

            --
            Martijn van Buul - pino@dohd.org - http://www.stack.nl/~martijnb/
            Geek code: G-- - Visit OuterSpace: mud.stack.nl 3333
            The most exciting phrase to hear in science, the one that heralds new
            discoveries, is not 'Eureka!' (I found it!) but 'That's funny ...' Isaac Asimov

            Comment

            • Frederick Gotham

              #21
              Re: How do i declare a byte variable?

              Martijn van Buul posted:
              >The extra efficiency of "unsigned int" is worth the cost of using up
              >another three bytes (or thereabouts).
              >
              Utter nonsense. There are plenty of reasons and plenty of applications
              where you *do* want to use a char if possible, and where it would even
              be *faster*.

              Show me where a char is faster than an int.

              Furthermore, given that a char is slower than an int, show me why you would
              want to use one for storing an integer (or than creating massive arrays).

              >Even if I were to write:
              >>
              > char unsigned i = 255;
              >>
              >, then it would still probably consume 4 bytes (or thereabouts) on most
              >systems.
              >
              *most* systems? Nonsense. Only on systems with a severe alignment
              problem.

              "char" doesn't have alignment requirements.

              >(If you have a system which can only access 4 bytes of memory at a
              >time, rather than one sole byte, then it would be efficient to store
              >the byte value in the least significant byte of the 4 bytes, and to
              >just ignore the values of the subsequent 3 bytes).
              >
              1) Those systems, should they exist, are rare.

              Don't a lot of Intel chips work like that? 486? Pentium?

              2) It would only be "efficient" from a minimum-execution-time point of
              view.

              Yes indeed, it would be quicker.

              It's grossly inefficient regarding memory usage.

              Three bytes is a drop in the ocean.

              >An easy test for this is the following:
              >>
              >#include <stdio.h>
              >>
              >int main(void)
              >{
              > struct FourBytes { char a,b,c,d; };
              >>
              > switch(sizeof(s truct FourBytes))
              > {
              > case sizeof(char[4]):
              > puts("Packed nice and tight!");
              > break;
              >
              This "proves" nor disproves absolutely nothing.

              It proves that the char's haven't been placed directly after each other in
              memory. Guess why.

              >Notwithstandin g any of this, your char is going to be promoted to int
              >every time you lay a finger on it.
              >
              Nonsense. Again.

              Perform arithmetic or equality on it and see what happens.

              My stance is this: Never use char unless memory is scarce.

              --

              Frederick Gotham

              Comment

              Working...