For Programmers: Free Programming Magazines  


Home > Archive > VC Language > June 2005 > Theoretical Question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Theoretical Question
Tim Roberts

2005-06-01, 8:59 pm

A colleague posed an interesting question to me today.

In C or C++, why can't I write this:

void MyFunction() {
unsigned var : 14;
// ...
}

The compiler already knows how to do the shifting and masking for bit
fields in structures. The above is nothing more than syntactic sugar for,
say:

void MyFunction2() {
struct {
unsigned f : 14;
} var;
// ...
}

But I could think of no good reason why the first one couldn't also be
done, and there are certain problem domains where that kind of thing could
prove useful.
--
- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc
Carl Daniel [VC++ MVP]

2005-06-01, 8:59 pm


"Tim Roberts" <timr@probo.com> wrote in message
news:r18s919ggm45m5a07fe2bj9rrupqb2vpcc@
4ax.com...
>A colleague posed an interesting question to me today.
>
> In C or C++, why can't I write this:
>
> void MyFunction() {
> unsigned var : 14;
> // ...
> }
>
> The compiler already knows how to do the shifting and masking for bit
> fields in structures. The above is nothing more than syntactic sugar for,
> say:
>
> void MyFunction2() {
> struct {
> unsigned f : 14;
> } var;
> // ...
> }
>
> But I could think of no good reason why the first one couldn't also be
> done, and there are certain problem domains where that kind of thing could
> prove useful.


One of a few reasons:
0. K&R C didn't work that way.
1. No one on the standards committee thought of it.
2. They did think of it, but didn't think it valuable enough, especially
given the workaround.

-cd


Simon Trew

2005-06-02, 4:02 am

Some languages designed for use in embedded systems, such as CORAL-66 and
Ada, do allow you to do this more directly. My guess is that since for
non-embedded systems bitmasking and shifting is relatively expensive, the
requirement of a structure is desirable on the grounds that expensive items
should look expensive.

(Many older embedded processors included sub-word processing as part of the
instruction set, and can operate on sub-word data without the need to do the
bitmask and shift as separate instructions. I stopped doing embedded
programming about nine years ago so I can't comment on modern embedded
processors.)

S.


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com