473,890 Members | 1,416 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

bool variable.(a non-standard question)

I have code which says

#define MYBOOL int

This code is very old. people who have written is not avaible. I was
thinking what could be reason.
1) bool datatype was not available that time (10 years back: correct me
if i am wrong)
2) int was word aligned (16bit and 32bit). Writter of above code doesnt
want to give choice to compiler.
if we say bool , compiler can represent this on his way (again,correct
me if i am wrong)
This much i can think of it.

even in many books, i have seen author using int as a flag. example
Richard Stevens, "advance programming in unix environment" . and this
book uses 'restrict' keyword.

Now, i have to change it for 64bit arch. In my system, performance is
more important thn anything else.
I thought of changing it to long on 64arch.
#define MYBOOL long

Any comment on this will be appreciated.

Sep 2 '06 #1
64 3916
shaanxxx wrote:
<snipped.
Now, i have to change it for 64bit arch. In my system, performance is
more important thn anything else.
I thought of changing it to long on 64arch.
#define MYBOOL long

Any comment on this will be appreciated.
Don't change it yet; run it first and change it
only if there actually is a performance hit.

--
goose
Have I offended you? Send flames to root@localhost
real email: lelanthran at gmail dot com
website : www.lelanthran.com
Sep 2 '06 #2
shaanxxx wrote:
I have code which says

#define MYBOOL int

This code is very old. people who have written is not avaible. I was
thinking what could be reason.
1) bool datatype was not available that time (10 years back: correct me
if i am wrong)
2) int was word aligned (16bit and 32bit). Writter of above code doesnt
want to give choice to compiler.
if we say bool , compiler can represent this on his way (again,correct
me if i am wrong)
This much i can think of it.

even in many books, i have seen author using int as a flag. example
Richard Stevens, "advance programming in unix environment" . and this
book uses 'restrict' keyword.

Now, i have to change it for 64bit arch. In my system, performance is
more important thn anything else.
I thought of changing it to long on 64arch.
#define MYBOOL long
So you want to declare a variable which will only
take the values 0 and 1 and you want its type to
be such that access will be as fast as possible , is
that right ?

Assuming I got it right then I would have to say that
it's out of topic. Ultimately the answer depends on
the processor you are using and perhaps also the
operating system. Your compiler presumably knows
about these things so read your compiler's documentation
and see if it says anything on how you should declare
variables in order to get the fastest possible access.

Sep 2 '06 #3
Spiros Bousbouras said:

<snip>
>
So you want to declare a variable which will only
take the values 0 and 1 and you want its type to
be such that access will be as fast as possible , is
that right ?
It seems likely. Of course, apart from a bitfield (of type unsigned int and
width 1), there is no C type that can /only/ take a value from the range
[0, 1], and a bitfield would be unlikely to win any awards for speed.

If speed is at a premium, he should choose int:

typedef int bool;

But if space is at a premium, he can decide how many bools he wants:

#define NUM_FLAGS 123 /* or whatever */

Then he can define an array of unsigned char:

#include <limits.h>

unsigned char flag[(NUM_FLAGS + CHAR_BIT - 1) / CHAR_BIT] = {0};

and then use bit-twiddling macros (e.g. see snippets.org) to get at
individual bits.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 2 '06 #4
"shaanxxx" <sh******@yahoo .comwrites:
I have code which says

#define MYBOOL int
[...]
Now, i have to change it for 64bit arch. In my system, performance is
more important thn anything else.
I thought of changing it to long on 64arch.
#define MYBOOL long
If you are using a modern implementation that has <stdint.h>, I
would suggest using int_fast8_t, which designates the fastest
signed integer type with at least 8 bits.
--
"I ran it on my DeathStation 9000 and demons flew out of my nose." --Kaz
Sep 2 '06 #5

Richard Heathfield wrote:
Spiros Bousbouras said:

<snip>

So you want to declare a variable which will only
take the values 0 and 1 and you want its type to
be such that access will be as fast as possible , is
that right ?

It seems likely. Of course, apart from a bitfield (of type unsigned int and
width 1), there is no C type that can /only/ take a value from the range
[0, 1], and a bitfield would be unlikely to win any awards for speed.

If speed is at a premium, he should choose int:

typedef int bool;
This is also true for 64bit arch ?

Sep 2 '06 #6
shaanxxx wrote:
>
I have code which says

#define MYBOOL int

This code is very old. people who have written is not avaible. I
was thinking what could be reason.
1) bool datatype was not available that time (10 years back:
correct me if i am wrong)
2) int was word aligned (16bit and 32bit). Writter of above code
doesnt want to give choice to compiler.
if we say bool , compiler can represent this on his way (again,
correct me if i am wrong)
C still has no bool type, unless you #include <stdbool.hin C99
systems. After which true and false will be macros that expand to
integers 1 and 0 respectively, and bool will expand to _Bool (which
is system defined, usually as an int).

However, since the dawn of time, C has defined logical expressions
(e.g. (x < y)) as returning either 0 or 1. This is why !!x can be
used to force the value to those, and why we can do arithmetical
tricks with booleans.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>

Sep 2 '06 #7
shaanxxx said:
Richard Heathfield wrote:
<snip>
>>
If speed is at a premium, he should choose int:

typedef int bool;

This is also true for 64bit arch ?
It has always been the intent of the powers-that-C that implementors should
choose a size of int that reflects the most natural object size for the
machine, and the most natural size is pretty darned likely also to be the
fastest size.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 2 '06 #8
Richard Heathfield wrote:
Spiros Bousbouras said:

<snip>

So you want to declare a variable which will only
take the values 0 and 1 and you want its type to
be such that access will be as fast as possible , is
that right ?

It seems likely. Of course, apart from a bitfield (of type unsigned int and
width 1), there is no C type that can /only/ take a value from the range
[0, 1], and a bitfield would be unlikely to win any awards for speed.

If speed is at a premium, he should choose int:

typedef int bool;
>From his opening post I gather that he thinks that
a 64 bit variable may be aligned in such a way as to
allow faster access than int. (I'm assuming that int
is 32 bits on his platform.)

Sep 2 '06 #9
Spiros Bousbouras said:

<snip>
>
(I'm assuming that int is 32 bits on his platform.)
Why?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 2 '06 #10

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

Similar topics

3
1706
by: Bryan Parkoff | last post by:
.....I try to reduce un-necessary temporal variables so it can be optimized for best performance. I try to assign only one register storage so two variables can access to only one register storage before they are stored into memory location (variable storage). .....Please take a look at two functions below. Byte variable and Carry variable are global variables outside of functions. .....Do you notice that there are two duplicated lines...
6
1358
by: arcticool | last post by:
Fields vs. Local Variables- as I understand a Field (declared inside a class) can share data across methods in the class, and to other classis (if declared public) where local variables (declared inside a method) are scoped only within the method. So my question is: what would be the point of declaring a local variable *public* ? Thanks, AC
2
1080
by: Bit byte | last post by:
I have a C function that takes variable args, i.e. is of the form : foo( const char*, const int, ... ) ; I want to expose this function so that I can call it from VB(6). My questions are: 1). Is it possible to export a function with variable args in a DLL? 2). How would such a functon be called from VB ? (Ok, this may not be the correct group for a question on VB but, it is related to what I'm
2
1268
by: mattdaddym | last post by:
Hi, I have a variable question in regards to my asp .net page. I need to declare a variable whose value is readable/writable to all of the subroutines of a specific page. So far I have one of two problems. The first is if I declare my variable right after the class definition, then I can use it anywhere, but the value does not persist from one subroutine to the next.
0
9979
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9826
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11234
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10829
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7171
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5854
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4682
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 we have to send another system
2
4276
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.