Short or Word?

This topic is 4421 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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?

Share on other sites
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...

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.

Share on other sites
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]

Share on other sites
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.

Share on other sites
Quote:
 Original post by NitageSHORT 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...

Share on other sites
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().

Share on other sites
for windows use either
signed	__int16unsigned __int16

Share on other sites
Ok, thanks for the help everyone. I think I'll go with __int16, since that seems like the safest way to go.

Share on other sites
Quote:
 Original post by SiCraneAssuming 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?

Share on other sites
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.

1. 1
2. 2
frob
15
3. 3
4. 4
Rutin
12
5. 5

• 12
• 12
• 58
• 14
• 15
• Forum Statistics

• Total Topics
632115
• Total Posts
3004195

×