CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

WORD keyword variable types

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
TimothyCarter



Joined: 08 Aug 2016
Posts: 22

View user's profile Send private message

WORD keyword variable types
PostPosted: Thu May 20, 2021 10:31 am     Reply with quote

NOTE see below for a couple of warnings about using WORD, and unions... use the following code at your own risk!

Just because I like using keywords such as WORD, DWORD and thought there just might be someone else that would like to as well:

I am not aware of any CCS libraries that do this, but it also comes in handy to access individual bytes/bits of variables.

It is HIGHLY recommended to use your own names for the unions, as the use of WORD, etc have been deprecated (see comment from Ttelmah below).

Also, I usually turn on case sensitivity (see the #case keyword).

Code:

/**************************************
 * File: var_def.h
 *
 * Description: Contains union defines
 *   for accessing individual bits
 *   of different size variables.
 *
 * Must define max bit depth of
 *   the processor used!
 *
 *************************************/

#ifndef _VAR_DEF_H
#define _VAR_DEF_H

// Need to have the stdint.h file for typedef's
#include <stdint.h>
#include <stdbool.h>

typedef union
{
   uint8_t Val;
   bool b[8];
} BYTE;

typedef union
{
   uint16_t Val;
   uint8_t B[2];
   bool b[16];
} WORD;

typedef union
{
   uint32_t Val;
   uint16_t W[2];
   uint8_t B[4];
   bool b[32];
} DWORD;

typedef unsigned long            word_t;
typedef unsigned int32           dWord_t;
typedef union
{
   dWord_t val;
   float fVal;
   word_t w[2];
   uint8_t  b[4];
} FLOAT_VAL;

#if defined(__PCD__)
typedef union
{
   uint64_t Val;
   uint32_t D[2];
   uint16_t W[4];
   uint8_t B[8];
   bool b[64];
} QWORD;
#endif

#endif      // End of _VAR_DEF_H

_________________
“Computer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter.”
- Eric S. Raymond


Last edited by TimothyCarter on Tue Apr 12, 2022 3:26 pm; edited 6 times in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon May 24, 2021 6:22 am     Reply with quote

It is a dangerous form to use.

Much better to always be explicit and use the forms like int16 etc..

Problem is that 'WORD' means a lot of different things. In Windows API's
they use 'word' for 16bits, which comes from the original 8086 (and in a
strange way the 8088 - which though an '8bit' CPU, used a 16bit WORD
size). However in processing, word is defined as:
Quote:

A WORD is the amount of data that a machine can process at one time. This fits into the document analogy that includes characters (usually eight bits) and pages (many words, often 4 or 8KB worth) as other measurements of data. A word is an integer number of bytes for example, one, two, four, or eight. When someone talks about the "n-bits" of a machine, they are generally talking about the machine's WORD size. For example, when people say the Pentium is a 32-bit chip, they are referring to its WORD size, which is 32 bits, or four bytes.


On the PIC16/18, a 'WORD' is actually 8bits, not 16bits.
In fact in some ways the PIC24's are 24bit WORD size chips. Using a 24bit
'instruction WORD', but still using a 16bit data WORD size.

For this reason, WORD though it existed in the early ANSI compilers was
removed from later releases, with the comment that it 'should not be used'.

You are 'making things harder for future compatibility.... Sad

Also BYTE is already a defined type.
TimothyCarter



Joined: 08 Aug 2016
Posts: 22

View user's profile Send private message

Old Code Examples
PostPosted: Tue Jun 22, 2021 11:07 am     Reply with quote

The above code is the result of using multiple compilers and trying to access the individual bytes in the 16, 32, and 64 bit variables: I realize CCS has things like make8, make16, etc - but like I say when switching between CCS and XC compilers, it gets confusing. Shocked

Since posting the above, I have also found out that unions should not be used for many of the same reasons you point out.

So, there needs to be a BIG disclaimer in the code Wink
_________________
“Computer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter.”
- Eric S. Raymond
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group