473,499 Members | 1,533 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

enum as char ?

i have this part of code:

typedef enum
{
OW_SEARCH_ROM = 0xF0,
OW_MATCH_ROM = 0x55,
OW_CONDITIONAL_SEARCH_ROM = 0xEC,
OW_HANDLE_SELECT = 0x0F
} ow_command_t;

ow_command_t command;
now "command" will be represented as int,
wich is a 16bit on my architecture. how can
i force the representation to be char ?
Nov 14 '05 #1
7 53315


d.********@raumcomputer.com wrote:
i have this part of code:

typedef enum
{
OW_SEARCH_ROM = 0xF0,
OW_MATCH_ROM = 0x55,
OW_CONDITIONAL_SEARCH_ROM = 0xEC,
OW_HANDLE_SELECT = 0x0F
} ow_command_t;

ow_command_t command;
now "command" will be represented as int,
wich is a 16bit on my architecture. how can
i force the representation to be char ?


You cannot. The compiler chooses an underlying
type for the enum, and you cannot control its choice.
(Well, in one sense you can: The underlying type must
be able to represent all the enumerated values, so
you can *prevent* the compiler from choosing `char' by
including a value like 0xB00B in the list. But that's
not the kind of "control" you want.)

I suggest you use the enum only to declare the
values of the enumerated constants, and use a separate
typedef:

enum { OW_ ... };
typedef unsigned char ow_command_t;
ow_command_t command;

Things like `command = OW_CONDITIONAL_SEARCH_ROM;' and
`if (command == OW_SEARCH_ROM)' will still work. The
only drawback I can think of is that some compilers will
issue warnings if you `switch' on an enumerated type and
do not provide `case' labels for all the values; these
warnings can be helpful.

switch (command) {
case OW_SEARCH_ROM: /* search ROM */
... break;
case OW_MATCH_ROM: /* set ROM aflame */
... break;
case OW_CONDITIONAL_SEARCH_ROM: /* maybe search
... break;
case OW_HANDLE_SELECT: /* select a handle */
... break;
}

A compiler that issued warnings for missing cases would
draw your attention to this error if `command' were an
enumerated type, but probably would not issue any warning
if `command' were an ordinary `unsigned char'.

--
Er*********@sun.com

Nov 14 '05 #2
previously it was codes via #define i changed that
to enum exactly besauce i want the compiler (gcc)
to produce a warning on incomplete switch statements.

just cause my architecture is 8-bit (Amtel) i need to have 8
bit-values here, 16-bit values result in larger and slower code ;(
Nov 14 '05 #3
d.********@raumcomputer.com writes:
previously it was codes via #define i changed that
to enum exactly besauce i want the compiler (gcc)
to produce a warning on incomplete switch statements.

just cause my architecture is 8-bit (Amtel) i need to have 8
bit-values here, 16-bit values result in larger and slower code ;(


Then you can use the 16-bit enum type for your switch statements:

enum ow_command {
OW_SEARCH_ROM = 0xF0,
OW_MATCH_ROM = 0x55,
OW_CONDITIONAL_SEARCH_ROM = 0xEC,
OW_HANDLE_SELECT = 0x0F
};

typedef unsigned char ow_command_t;

...

ow_command_t foo = whatever;
switch((enum ow_command)foo) {
case OW_SEARCH_ROM: ...
case OW_MATCH_ROM: ...
case OW_CONDITIONAL_SEARCH_ROM: ...
case OW_HANDLE_SELECT: ...
}

Note that this will convert the value of foo (8 bits) to type enum
ow_command (16 bits) for the switch statement. If that's going to be
a performance problem, you can define a conversion macro that either
does the conversion (to get the warnings) or that does nothing (for
greater performance); you can control how the macro is defined at
compilation time:

enum ow_command {
OW_SEARCH_ROM = 0xF0,
OW_MATCH_ROM = 0x55,
OW_CONDITIONAL_SEARCH_ROM = 0xEC,
OW_HANDLE_SELECT = 0x0F
};

typedef unsigned char ow_command_t;

...

#ifdef WANT_WARNINGS
#define ENUM_OW_COMMAND(x) ((enum ow_command)(x))
#else
#define ENUM_OW_COMMAND(x) (x)
#endif

ow_command_t foo = whatever;
switch (ENUM_OW_COMMAND(foo)) {
case OW_SEARCH_ROM: ...
case OW_MATCH_ROM: ...
case OW_CONDITIONAL_SEARCH_ROM: ...
case OW_HANDLE_SELECT: ...
}

Warning: I haven't tried compiling any of this; it may be full of
typos and/or stupid errors.

All of this (potentially) makes your generated code smaller and faster
at the expense of obfuscating your source code. Like any manual
optimization, you should actually measure the performance before
deciding whether the tradeoff is worthwhile. You may even find that
your compiler, with the proper options, can do a better job of
micro-optimization than you can.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #4
In article <39*************@individual.net>, d.********@raumcomputer.com wrote:
just cause my architecture is 8-bit (Amtel) i need to have 8
bit-values here, 16-bit values result in larger and slower code ;(


I would complain to the vendor if a compiler for an eight-bit target
were generating 16-bit accesses for an enum that had eight-bit values.
I don't run into this problem with eight-bit compilers I use.

--
John W. Temples, III
Nov 14 '05 #5
Hi,

I'm wondering about how to use enum.

It's up to the compiler to determine what integer type the enum is, in
C. Basically it's an int, but the enums I'm using are all with literal
values less than 256, i.e. they fit in an octet or 8-bit byte.
Then, when the enum is defined, there's something like this, with
regards to previous considerations about struct_tag and s_cast:

#ifndef use_enums

#define A 0
#define B 1
#define C 2

typedef uint_8 X_t;

#define e_cast8(x) x
#define e_cast(x)

#endif

#ifdef use_enums

enum_kw(typedef) enum enum_tag(X_t){
A = 0,
B = 1,
C = 2
} enum_td(X_t);

#define e_cast8(x) s_cast(uint_8)(x)
#define e_cast(x) s_cast(x)

#endif

Then, with these ugly preprocesor definitions, the user can configure
at the single-point-of-change file whether to use enums or not, and
whether to use the tag and typedef and stuff or not, at compile time.

X_t letter = A;
uint_8 c;
switch(letter){
case A:
c = e_cast8(letter)
break;
case B:
c = e_cast8(letter)
break;
case C:
c = e_cast8(letter)
break;
default:
break;
}

Anyways my idea about compiling with enums or not here is probably
again useless. The only reason I like enums is because in the debugger
it shows as the enum instead of the integer, but in general it is not
possible to standardly demand the enum be any type other than at least
some type in which it fits. I just need the enumerated value to be a
fixed width, preferably the least necessary integer multiple of eight
bits.

A problem with the macros in the type definitions is that some
automatic code documentations generators don't, or rather, doxygen
doesn't, process the macro so the types don't appear in the listings.
So I'm wondering whether to it is worth that, or to selectively
preprocess those macros before documentation generation.
This is about a separate issue, but it took a bunch of iterations to
figure out how to selectively include a file based upon concatenation
of some identifier fragment with the string. I use:

#define PLATFORM_BYTE_ORDER_INCLUDE(x) #x "_be.h"

Then I can use

#include
PLATFORM_BYTE_ORDER_INCLUDE(function_with_several_ encoded_variations)

to say

#include "function_with_several_encoded_variations_be.h "

I think that works when a variety of other combinations about passing a
quoted string to #include don't for some reason, it merges the
resulting strings _before_ passing it to #include, but I don't know how
it will behave in the general case for preprocessors, or if there is
some canonical way to do that.

Regards,

Ross F.

Nov 14 '05 #6
omg ! my primary goal was to make the code easier to understand
and have warnings about missing cases. theese macroes do make
it harder to read.
i just got back to the good old #defines. i checked everything about
my compiler, if theres an option to use 8bit enums but didn't find any.
Nov 14 '05 #7
i am using a modified gnu c version. the 8bit - 16bit issues are well
known by the designers and it occurs in some other areas.

for example

char foo(char);

parameter as well as return value are passed through 2 8bit registers.
I can't complain about this, cause i know the developers do the best
they can in their free time. I just hope that this will be fixed someday.
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2231
by: Rafal 'Raf256' Maj | last post by:
Hi, how can I make operator that will automaticly converse tMyEnum into char, typedef enum { ... } tMyEnum; void Fun(char c) { ... } tMyEnum e; Fun( e );
9
3118
by: AngleWyrm | last post by:
"The C++ Programming Language" by Bjarne Stroustrup, copyright 1997 by AT&T, section 4.8 (pp 77): "A value of integral type may be explicitly converted to an enumeration type. The result of such a...
2
2410
by: Kelvin | last post by:
In Visual Studio we can limit the size of an enum by doing: enum Weekday : char { MON, TUE, WED; }; is that in the stardard C++ ? or it's just an extension from microsoft?
28
2667
by: Merrill & Michele | last post by:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(void){ char *p; p=malloc(4); strcpy(p, "tja"); printf("%s\n", p); free(p); return 0;
18
2342
by: Nebula | last post by:
Consider enum Side {Back,Front,Top,Bottom}; enum Side a; Now, why is a = 124; legal (well it really is an integer, but still, checking could be performed..) ? Wouldn't enums be more useful if...
6
4639
by: randy1200 | last post by:
The following enum is given to me, and I can't change it: enum yo { ONE, TWO, THREE }; I have the following: char test = "ONE"; Any ideas on how to see if the string in "test" is in the...
10
23546
by: kar1107 | last post by:
Hi all, Can the compiler chose the type of an enum to be signed or unsigned int? I thought it must be int; looks like it changes based on the assigned values. Below if I don't initialize...
35
15140
by: dtschoepe | last post by:
Greetings. I am working on an assignment and can't seem to get the right concept for something I'm attempting to do with enum data types. I have defined the following in my code: enum color...
7
8244
by: Travis | last post by:
Just curious, which takes less memory? enum Months { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC }; static const char *Months =
16
6422
by: andreyvul | last post by:
If I try compiling this in gcc, it says: "error: request for member `baz' in something not a structure or union". Any workarounds or tips on how to make a structure such that it behaves like an...
0
7130
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7171
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5468
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4599
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3098
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
664
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
295
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.