8 bit integer type

Discussion in 'C++' started by zacariaz, Aug 29, 2007.

  1. zacariaz

    zacariaz Guest

    typedef char int(myint8);

    the above is realy what i wanna do, but ofcourse it cant be done this
    way. I have looked around for a 8 bit integer type, but no luck. the
    int8_t and simular still acts like chars.
    Yes i know it a stupid little thing, but im getting tired of writing
    int(my8bitvar); all the time, and if there is a type that doesnt act
    like a char, i wanna know.

    I have allso tryed defining my own type, but got lost when i had to
    implement all the operator (==, -=, +=, ++, --, etc.) and besides it
    seemed a really stupid way to go about it.

    So please i anyone holds an answer, let me hear it.

    Regards
     
    zacariaz, Aug 29, 2007
    #1
    1. Advertisements

  2. In what way does a char differ in behaviour from an int, except for the
    range of values it can hold?
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Aug 29, 2007
    #2
    1. Advertisements

  3. zacariaz

    zacariaz Guest

    Of course it doesnt differ much, but it does differ, fx. what do you
    think happened when i first head about the stdint.h and wanted to try
    out the uint8-t?

    @code
    uint8_t var = 7;
    std::cout << var;
    @code

    BEEP! it said.
    Imagine my surprise. and integer type making funny noises?


    Yes, it is a tiny little wee wee thing that nobody care about, but
    still it enoys me having to write
    std::cout << int(var);
    Yes, it is only 5 extra letter, well that depends, but the goal of
    this post was only to make certain that a real 8 bit integer type or
    an easy soluton doesnt allready excist.
     
    zacariaz, Aug 29, 2007
    #3
  4. Yes, I see. This is of course because uint8_t is just a typedef of an
    unsigned char. I don't think anyone but the compiler vendor or library
    vendor can fix this.
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Aug 29, 2007
    #4
  5. zacariaz

    anon Guest

    struct int8bit
    {
    unsigned int number : 8;
    }

    then implement a class which does everything you want to do with this.
     
    anon, Aug 29, 2007
    #5
  6. zacariaz

    zacariaz Guest

    ah yes, two problems reamins however.
    1. How much space does this new type use in memory? i mean is it just
    an 32 bit int with an 8 bit limitation or is it infact an 8 bit int?
    2. this vould not be used as a regular type but would have to be
    called like int8bit.number, this is not that important, but really it
    would be tha same as writing int(char), eg. more enoying code.
     
    zacariaz, Aug 29, 2007
    #6
  7. zacariaz

    anon Guest

    It would take 32 bit memory, but it would be 8-bit unsigned int.
    You could encapsulate it in a class.
     
    anon, Aug 29, 2007
    #7
  8. zacariaz

    zacariaz Guest

    not that it matters much cause the solution is not what im looking
    for, but what do you mean by "encapsulate in a class"?
     
    zacariaz, Aug 29, 2007
    #8
  9. zacariaz

    anon Guest

    You can do the same for char (or unsigned char, not sure what you
    using). By encapsulating it in a class, you would have to overload all
    operators you are going to use (+, - , etc) and you would have to write
    operator<< for that class. That way you would not have to cast when
    sending to a stream.
     
    anon, Aug 29, 2007
    #9
  10. Hi!

    which can be reduced, but would also reduce the verbosity:

    std::cout << +var;

    The unary operator + will implicitly cast the value to int. Hard to
    read, though, so it's not good for maintenance.

    Frank
     
    Frank Birbacher, Aug 30, 2007
    #10
  11. zacariaz

    Geoff Guest

    Wouldn't compilers still align data on word or half-word bounderies in
    memory, so using a char doesn't really save any bits?

    Or I could be imagining things.

    --n
     
    Geoff, Aug 31, 2007
    #11
  12. Please don't top-post and don't quote signatures.
    Yes you are, you are imagining that all computers work the same way,
    imagine for example a computer that can align on half-word boundaries
    and have a 16 bit word, that means no padding and no loss.
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Aug 31, 2007
    #12
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.