473,388 Members | 936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Are bit-variables in PICC a good idea

Hi!

I had a discussion in the other day about the usefullness and validity of
the following (non-std) C extension in PICC:

static bit BitVar @ 0x20*8+0;

This would mean that a static variable, named BitVar will be located in the
memory at address 0x20 at bit 0. (Note, that this C compiler is for an 8-bit
microcontroller and this construct should provide a means to define
HW-related functions in C.)

With this declaration the following program would set bit 0 of address 0x20
to 0 and bit 1 to 1:

static bit BitVar1 @ 0x20*8+0;
static bit BitVar2 @ 0x20*8+1;

void main() {
BitVar1 = 0;
BitVar2 = 1;
}

The discussion was about two things:
- Since there are other (though more complex) constructs in standard C to
achieve the same effect, is it a good idea to add a new built-in type
(without the __ prefix) and an address-binding syntax (with the @ operator)
to the language?
- Since all operations on all modern CPUs are work on at least 8-bits of
data at once, is it even possible to compile such a source program into a
valid executable (in a sense that it accesses only memory that's defined in
the source)?

We couldn't on our own reach an agreement so I seek the opinions of a wider
audience.

--
Thanks,
Andras Tantos
<http://andras.tantos.homedns.org>
Nov 14 '05 #1
3 5062
It really depends on whether your priority is efficiency or portability (or
a compromise between these)
A good technique is to use a typedef based on the platform, something like :

#if __TARGET__ == __SOME_TARGET_WHOSE_COMPILER_SUPPORTS_BITS__
#else
typedef int bit ; /* or is it bit int - I can't remember */
#endif

You can then use the C convention that 0 is false and anything else is true
to make your code portable.

It is almost always a bad idea to define absolute addresses as "magic
numbers" in source code. The link process should give you a much more
maintainable and portable way to do this

"Andras Tantos" <an***********@yahoo.com> wrote in message
news:_U*****************@nwrddc03.gnilink.net...
Hi!

I had a discussion in the other day about the usefullness and validity of
the following (non-std) C extension in PICC:

static bit BitVar @ 0x20*8+0;

This would mean that a static variable, named BitVar will be located in the memory at address 0x20 at bit 0. (Note, that this C compiler is for an 8-bit microcontroller and this construct should provide a means to define
HW-related functions in C.)

With this declaration the following program would set bit 0 of address 0x20 to 0 and bit 1 to 1:

static bit BitVar1 @ 0x20*8+0;
static bit BitVar2 @ 0x20*8+1;

void main() {
BitVar1 = 0;
BitVar2 = 1;
}

The discussion was about two things:
- Since there are other (though more complex) constructs in standard C to
achieve the same effect, is it a good idea to add a new built-in type
(without the __ prefix) and an address-binding syntax (with the @ operator) to the language?
- Since all operations on all modern CPUs are work on at least 8-bits of
data at once, is it even possible to compile such a source program into a
valid executable (in a sense that it accesses only memory that's defined in the source)?

We couldn't on our own reach an agreement so I seek the opinions of a wider audience.

--
Thanks,
Andras Tantos
<http://andras.tantos.homedns.org>

Nov 14 '05 #2
Gary Pace wrote:
typedef int bit ; /* or is it bit int - I can't remember */


The syntax for typedef declarations is the same as for "normal"
declarations. The "typedef" keyword is placed where you might
otherwise place a storage-class specifier such as "static" or
"extern". So,

/* declare an variable `a' of type int */
static int a;

/* declare a type alias `b' for type int */
typedef int b;

/* declare a function `f', which returns a pointer to a function
which returns a pointer to char */
extern char *(*f())();

/* declare a type alias `f' for a function which returns a pointer
to a function which returns a pointer to char */
typedef char *(*f())();

Jeremy.
Nov 14 '05 #3
On Mon, 12 Jan 2004 02:47:22 GMT, "Andras Tantos"
<an***********@yahoo.com> wrote:

[...]
static bit BitVar1 @ 0x20*8+0;
static bit BitVar2 @ 0x20*8+1; [...]- Since there are other (though more complex) constructs in standard C to
achieve the same effect, is it a good idea to add a new built-in type
(without the __ prefix) and an address-binding syntax (with the @ operator)
to the language?
These are two separate issues:

1) Double-underbar prefix. Keep it. If you (or your user) wants to
hide the "ugliness", use a macro defined in a header a la _Bool and
bool in stdbool.h

2) The only real feature this provides over _Bool is the ability to
specify the location of the bit in memory. And the only reasons to do
this (that I can imagine) are to specify control and status bits in
hardware registers, or to assemble bit masks into bytes or words in
order to handle packages of bits. For both these operations I prefer
masking and shifting operations because the actual operations used and
the order they are taken can be (must be) specified in the code, so
(for example) possible interactions with interrupt service routines
are more visible.

FWIW, I generally avoid bitfield structures except when 1) the
compiler doesn't support a native bit type like _Bool, and 2) I have
several boolean variables I need to define that are not necessarily
related to one another. Something like the following:

struct bits {
unsigned b0 :1;
unsigned b1 :1;
unsigned b2 :1;
unsigned b3 :1;
unsigned b4 :1;
unsigned b5 :1;
unsigned b6 :1;
unsigned b7 :1;
} my_status_bits;

#define boost_switch_position my_status_bits.b0
#define is_warmup_mode my_status_bits.b1
/*etc.*/

But even then, you have to be careful about using these in ISRs.
Something like

is_warmup_mode = TRUE;

isn't generally atomic.

- Since all operations on all modern CPUs are work on at least 8-bits of
data at once, is it even possible to compile such a source program into a
valid executable (in a sense that it accesses only memory that's defined in
the source)?


What's a "modern CPU?"

For the PIC or an 8051, with memory-mapped registers, it makes sense.
For a Pentium running Windoze, it doesn't.

HTH,

-=Dave
--
Change is inevitable, progress is not.
Nov 14 '05 #4

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

Similar topics

7
by: Bonj | last post by:
Happy new year to everybody... Just a few questions about porting code to 64-bit environment. 1) Is there a different version of the SDK (i.e., C/C++ compiler) for the 64-bit platform, or does...
13
by: Amy DBA | last post by:
I've been asked to administer a DB2 V 8 (32-bit install) on a Solaris 64-bit platform. It seems like whomever installed DB2 on the server, goofed for not installing DB2 v8 64 bit. Do I understand...
12
by: Jean-Marc Blaise | last post by:
Hi, Is it worth to use 64-bit DB2 instances on a 32-bit kernel, in terms of: - performance - configuration (go beyond the 256 Mb segment for private mem, 1.75 Gb for Bufferpools) - other ? ...
4
by: Allan Bruce | last post by:
Hi there, Would the following functions work for bit manipulation? I want to specify a byte and the bit of the byte (from the MSB down to LSB) and either set it, clear it or get what it is...
16
by: Mohanasundaram | last post by:
Hi All, We are working on porting a product written in C and C++ from 32 bit to 64 bit. We need to maintain both 32 bit and 64 bit versions in the future. We took the 32 bit source code and...
112
by: Carsten Hansen | last post by:
Suppose I'm using an implementation where an int is 16 bits. In the program below, what function is called in the first case, and what is called in the second case? Also, if there is a difference...
11
by: JDeats | last post by:
1. Will there be different 64-bit .NET implementations for Intel and AMD 64-bit processors or will they share a common 64-bit CLR? 2. Will .NET managed code compiled for the 32-bit CLR be binary...
58
by: Larry David | last post by:
Ok, first of all, let's get the obvious stuff out of the way. I'm an idiot. So please indulge me for a moment. Consider it an act of "community service".... What does "64bit" mean to your friendly...
4
by: tommydkat | last post by:
Well, I've finally gotten UDB 8.2 FixPak 3 up and running on my HP-UX 11i system, thanks to Mr McBride and IBM support. :) I created a 32-bit instance and that's running just fine. However, I...
1
by: Chuck Chopp | last post by:
I have some code that is being built on the following: Windows Server 2003, both 32-bit & 64-bit editions Windows Vista, both 32-bit & 64-bit editions Windows Server 2008, both 32-bit & 64-bit...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.