SRL Archive - C/C++ Help and Tutorials

Declaring new data type


Repentinus

2008-04-09 16:20:45

Hi!
How can I define new data type with 8 bit size and it should allow to store:
a) unsigned integers;
b) signed integers?

Thank you,
Repentinus

R0b0t1

2008-04-09 17:55:43

typedef blahh union{signed integer; unsigned integer};

Bobarkinator

2008-04-09 18:50:15

What about struct?

R0b0t1

2008-04-09 21:54:45

With a struct it would hold both memory types - sounds like he wants one or another.

Repentinus

2008-04-10 08:26:24

Which part of this code says that it is 8 bit long?

Yakman

2008-04-10 11:40:47

it doesnt,
integers are normally the word-size, so 32 bits on my computer
bytes have 8 bits

so you would probably do

byte mySignedByte;
unsigned byte myUnsignedByte;

Repentinus

2008-04-10 14:33:21

template <int N> struct A {
unsigned a: N;
};

int main() {
A <5> bit5;
A <13> bit13;
return 0;
}

Would something like this work too? (I know that here are different values.)

R0b0t1

2008-04-10 17:04:32

I say your being confusing… You've said you wanted un/signed integers in one type, now what are you talking about?

Repentinus

2008-04-11 06:18:41

My mistake, I actually meant my first post as how I can define two different datatypes signed and unsigned. Would example in my previous post work this way?

boberman

2008-04-17 11:35:32

From: Repentinus
My mistake, I actually meant my first post as how I can define two different datatypes signed and unsigned. Would example in my previous post work this way?


So, are you trying to change the data type, or are you trying to define it at run time? Your code makes absolutely no sense whatsoever.

If you just want to make a variable signed or unsigned then declaring them like this should work

unsigned int noSign;
int noSign;

If you are trying to be able to convert from one to another then I would probably suggest a class with two functions, signed and unsigned that will take its input and convert it to signed or unsigned depending on what you want.