Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,661 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 3,644 people online right now. Registration is fast and FREE... Join Now!




short int

 
Reply to this topicStart new topic

short int, is there anything smaller c++

yogi
post 28 Dec, 2005 - 11:01 AM
Post #1


New D.I.C Head

*
Joined: 25 Dec, 2005
Posts: 10



Thanked 1 times
My Contributions


Hi
a part of my c++ program will only ever need to the integers from 0 to 140;

Im using linux fc4
A short int has a size of 2 bytes. Im storing alot of these integers and was wondering if its possible to restrict the size of these ints to 256 bits.

thanks yogi
User is offlineProfile CardPM

Go to the top of the page


microchip
post 28 Dec, 2005 - 01:49 PM
Post #2


New D.I.C Head

*
Joined: 14 Aug, 2005
Posts: 37



Thanked 3 times
My Contributions


You mean restricting the size of the int's to 8 bit's right (that's 0-255).

Sure you've got a type for that: char. It works exactly like all the others, only it's normally used to save characters. But you can just as well use it for numbers:

unsigned char myNumber = 50;

If you want to output it as a number with cout, you'll have to typecast it to int first:

cout << (int)myNumber << endl;

Just for clarity, I always declare the type 'byte' if I want a 8-bit variable:

typedef unsigned char byte;

That allows me to use char for letters, and byte for numbers.
User is offlineProfile CardPM

Go to the top of the page

yogi
post 28 Dec, 2005 - 07:27 PM
Post #3


New D.I.C Head

*
Joined: 25 Dec, 2005
Posts: 10



Thanked 1 times
My Contributions


Hey thanks for the quick reply.

Sorry for possibly making you repeat yourself but would this mean that to do normal integer arithmatic such as 1+ 2 or 1 * 2, i would keep having to cast it to int before using it, and then casting it back to char.

Guess wat I really want to know is the trade off, in saving memory over time. I thought if i had an int which is only 1 byte big , then adding say 1 + 2 would quicker than adding 1 + 2 with a 2 byte int. So in turn not only would i save on memory but on time.

However at this stage i am more interested in the time consumption. So if i have to typecast it as described above than WOULD these additional operations of typecasting be slower than just adding two ints together.

My program is estimated to take 100 hours to complete. I have to reduce this down to about 24 - 48 hours. This is just a small improvement but i guess every little helps!?! biggrin.gif

Thanks again yogi
User is offlineProfile CardPM

Go to the top of the page

microchip
post 29 Dec, 2005 - 05:11 PM
Post #4


New D.I.C Head

*
Joined: 14 Aug, 2005
Posts: 37



Thanked 3 times
My Contributions


No no! Normal arithmatic works fine on chars. You only have to cast it to view the output, because when cout gets a char variable, it shows it as an ASCII value.

As for speeding up code: It all depends on what platform you're working on. If it's a 'normal' x86 32-bit processor, using variables with sizes other than 32 bits will slow things down.
I'm not entirely sure though, since using variables with smaller sizes does decrease the RAM usage...

The best way to find out, is to try it of course biggrin.gif

This post has been edited by microchip: 29 Dec, 2005 - 05:11 PM
User is offlineProfile CardPM

Go to the top of the page

yogi
post 30 Dec, 2005 - 08:55 AM
Post #5


New D.I.C Head

*
Joined: 25 Dec, 2005
Posts: 10



Thanked 1 times
My Contributions


Thanks, my work has occured more problems, but when im ready to test again, i'll post up the results.

Yogi
User is offlineProfile CardPM

Go to the top of the page

gregoryH
post 13 Oct, 2006 - 04:12 PM
Post #6


D.I.C Regular

Group Icon
Joined: 4 Oct, 2006
Posts: 417



Dream Kudos: 50
My Contributions


QUOTE(microchip @ 29 Dec, 2005 - 06:11 PM) *

No no! Normal arithmatic works fine on chars. You only have to cast it to view the output, because when cout gets a char variable, it shows it as an ASCII value.

As for speeding up code: It all depends on what platform you're working on. If it's a 'normal' x86 32-bit processor, using variables with sizes other than 32 bits will slow things down.
I'm not entirely sure though, since using variables with smaller sizes does decrease the RAM usage...

The best way to find out, is to try it of course biggrin.gif


micro

using 8 bits out of 32 may not reduce your memory usage as the data "alignment" may still take up 4 bytes....

some compilers will allow you to align your data to byte, DWORD etc.. check your project options
User is offlineProfile CardPM

Go to the top of the page

born2c0de
post 14 Oct, 2006 - 01:07 AM
Post #7


printf("I'm a %XR",195936478);

Group Icon
Joined: 26 Nov, 2004
Posts: 3,895



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


You can use the __int8 data type which is 1 byte long.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 06:46AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month