8 bit integer type

Z

zacariaz

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
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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.

In what way does a char differ in behaviour from an int, except for the
range of values it can hold?
 
Z

zacariaz

In what way does a char differ in behaviour from an int, except for the
range of values it can hold?

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.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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.

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.
 
A

anon

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.

struct int8bit
{
unsigned int number : 8;
}

then implement a class which does everything you want to do with this.
 
Z

zacariaz

struct int8bit
{
unsigned int number : 8;

}

then implement a class which does everything you want to do with this.- Skjul tekst i anførselstegn -

- Vis tekst i anførselstegn -

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.
 
A

anon

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?

It would take 32 bit memory, but it would be 8-bit unsigned 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.

You could encapsulate it in a class.
 
Z

zacariaz

It would take 32 bit memory, but it would be 8-bit unsigned int.


You could encapsulate it in a class.- Skjul tekst i anførselstegn -

- Vis tekst i anførselstegn -

not that it matters much cause the solution is not what im looking
for, but what do you mean by "encapsulate in a class"?
 
A

anon

not that it matters much cause the solution is not what im looking
for, but what do you mean by "encapsulate in a class"?

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.
 
F

Frank Birbacher

Hi!

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,

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
 
G

Geoff

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
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Please don't top-post and don't quote signatures.
> 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.

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top