Short or Word?

Started by
9 comments, last by Colin Jeanne 17 years, 9 months ago
Hi, I'm writing a program that writes data to a USB module, which will then transfer that data to a Digital-to-Analog convertor over the SPI interface. (If you don't know what all that means it's not important). I need to send 16 bits of information to the DAC. I would like my program to be as universal as possible on Windows machines (so it always sends only 16 bits), so would I use a short, a SHORT, or a WORD? Or does it not matter?
Advertisement
Ok typically a word on a 32-bit system is 4 x 8 bits and on a 64-bit system 8 x 8 bits. I believe that the standard for a short is always 16 bit...

From: http://cplus.about.com

Definition: Shorts are an integral data type used to store integers. They are stored in 16 bits on 32-bit machines, and 32 bits on 64 bit machines. Please see int for more details.
SHORT and WORD aren't standard types, however:
The SHORT from windows.h will always be 16 bits.
The WORD from windows.h will always be 32 bits (edit: 16 bits apparently)

The C standard doesn't guarentee the size of short, but I'm not aware of a compiler for the windows platform that doesn't use a 16bit short.

[Edited by - Nitage on July 7, 2006 10:40:08 AM]
Assuming C++, you can use boost/cstdint.hpp and use the uint16_t or int16_t types, depending on whether you want a signed or unsigned integer.

However, a SHORT and a WORD are both 16 bits in the Win32 API.
I did a sizeof(short),a sizeof(WORD), and a sizeof(SHORT), and they all returned 2 bytes. However, this is a 32-bit computer. I'm using windows.h anyway, so from what Nitage said, SHORT seems like what I need. If my program runs on a computer with a 64 bit processor on Windows 64-bit, will the size of SHORT be different?

EDIT: Ha, raz0r, we posted the exact same second. What are the odds?

And yeah, I thought WORD was 16 bits, because that's what I got when I did sizeof().
Quote:Original post by Nitage
SHORT and WORD aren't standard types, however:
The SHORT from windows.h will always be 16 bits.
The WORD from windows.h will always be 32 bits.

Correct me if I'm wrong, but as far as I know, on 32-bit Windows:
- SHORT is a typedef for short, which is 16-bits.
- WORD is a typedef for unsigned short, which should also be 16-bits.

Edit: Too late...
for windows use either
signed	__int16unsigned __int16
Ok, thanks for the help everyone. I think I'll go with __int16, since that seems like the safest way to go.
Quote:Original post by SiCrane
Assuming C++, you can use boost/cstdint.hpp and use the uint16_t or int16_t types, depending on whether you want a signed or unsigned integer.


Is that really boost? cstdint comes by default with gcc. I thought it was C99?

stdint.h is a C99 header. boost/cstdint.hpp is a boost implementation of the same header for compilers that don't come with the stdint.h header.

This topic is closed to new replies.

Advertisement