Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > array of bits

Reply
Thread Tools

array of bits

 
 
\\
Guest
Posts: n/a
 
      05-13-2010
Hello,
I would like to implement array of bits using the appropriate data
type for the machine
in which the code is compiled. So, using 32 bits for 32 bits machines
(and os), and 64 bits
for 64 bits machines (and os).
How can I know which is the natural word size of the machine and do
all the rest?
Thanks,
tano
 
Reply With Quote
 
 
 
 
Tom St Denis
Guest
Posts: n/a
 
      05-13-2010
On May 13, 12:39*pm, "\"\"" <(E-Mail Removed)> wrote:
> Hello,
> I would like to implement array of bits using the appropriate data
> type for the machine
> in which the code is compiled. So, using 32 bits for 32 bits machines
> (and os), and 64 bits
> for 64 bits machines (and os).
> How can I know which is the natural word size of the machine and do
> all the rest?
> Thanks,
> * * * * * * * * * * *tano


Make an array of unsigned int then use CHAR_BIT * sizeof(int) to
figure out how many bits are in it.

Tom
 
Reply With Quote
 
 
 
 
\\
Guest
Posts: n/a
 
      05-13-2010
Well, I hope there is a better way.
I think that even a 64 bit machine could use 32 bit registers for
unsigned int.
In 64 bit machines, I would like to use 64 bit data types.
Since I have to to AND and OR operation between bitsets, I think there
is a
good point in using 64 bits when available.
Is it possible that there is no way to get if I am compiling under a
64 bit machine?
 
Reply With Quote
 
Dann Corbit
Guest
Posts: n/a
 
      05-13-2010
In article <dcf74d76-a93c-480d-b657-43b2bd116812
@o14g2000yqb.googlegroups.com>, http://www.velocityreviews.com/forums/(E-Mail Removed) says...
>
> Hello,
> I would like to implement array of bits using the appropriate data
> type for the machine
> in which the code is compiled. So, using 32 bits for 32 bits machines
> (and os), and 64 bits
> for 64 bits machines (and os).
> How can I know which is the natural word size of the machine and do
> all the rest?
> Thanks,
> tano



It's a FAQ:

20.8: How can I implement sets or arrays of bits?

A: Use arrays of char or int, with a few macros to access the
desired bit at the proper index. Here are some simple macros to
use with arrays of char:

#include <limits.h> /* for CHAR_BIT */

#define BITMASK(b) (1 << ((b) % CHAR_BIT))
#define BITSLOT(b) ((b) / CHAR_BIT)
#define BITSET(a, b) ((a)[BITSLOT(b)] |= BITMASK(b))
#define BITTEST(a, b) ((a)[BITSLOT(b)] & BITMASK(b))

(If you don't have <limits.h>, try using 8 for CHAR_BIT.)

References: H&S Sec. 7.6.7 pp. 211-216.

BTW, the book has a more detailed answer. If you want to actually
implement it as an array, you will have to use C++, because C does not
have operator overloading.

But if you are using C++ then it is already done for you in the STL
bitset template.
 
Reply With Quote
 
\\
Guest
Posts: n/a
 
      05-13-2010
Yes, I read the FAQ.

My doubts are not how to do operations, but how can I choose
the most appropriate type for bitset.
I.e. choose a 32 bits type on a 32 bits machine,
and a 64 bits type on a 64 bits machine.
However thanks,
tano
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-13-2010
"\"\"" <(E-Mail Removed)> writes:
> Well, I hope there is a better way.
> I think that even a 64 bit machine could use 32 bit registers for
> unsigned int.
> In 64 bit machines, I would like to use 64 bit data types.
> Since I have to to AND and OR operation between bitsets, I think there
> is a
> good point in using 64 bits when available.
> Is it possible that there is no way to get if I am compiling under a
> 64 bit machine?


(Please find a way to format your paragraphs more sanely. The jagged
lines are difficult to read.)

What exactly is a "64 bit machine"? If you can rigorously define the
term (I can't), then you can probably figure out a way to determine
whether you're on one.

My best suggestion: Look at a variety of systems, and for each one,
see which integer type is the one you'd like to use. Ideally
unsigned int should be the best choice, since it's *supposed*
to have "the natural size suggested by the architecture of the
execution environment" (C99 6.2.5p5), but requirements for backward
compatibility with 32-bit systems often override this suggestion.
Some supposedly 64-bit systems even have 32-bit longs.

It may be that unsigned long will be the best choice on a wide
variety of systems, or perhaps one of the int*fast_t types defined in
<stdint.h> (for implementations that provide <stdint.h>). Or make it
a build-time configuration option, chosen manually for each target.

You should also profile your code with both 32-bit and 64-bit
integers on each platform. You might be surprised by the results.
I don't have any particular surpise in mind, I'm just saying that
you shouldn't make assumptions.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      05-13-2010
"\"\"" <(E-Mail Removed)> writes:

> My doubts are not how to do operations, but how can I choose
> the most appropriate type for bitset.
> I.e. choose a 32 bits type on a 32 bits machine,
> and a 64 bits type on a 64 bits machine.


I would probably just use "unsigned long". In the most common
cases it will have the sizes that you request, and in other cases
it will still work.
--
Ben Pfaff
http://benpfaff.org
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      05-13-2010
On 5/13/2010 3:48 PM, "" wrote:
> Yes, I read the FAQ.
>
> My doubts are not how to do operations, but how can I choose
> the most appropriate type for bitset.
> I.e. choose a 32 bits type on a 32 bits machine,
> and a 64 bits type on a 64 bits machine.


If you can define precisely what you mean by "32 bits machine"
and "64 bits machine," perhaps someone will think of a test you can
make to tell them apart. My bet is that a rigorous definition will
prove to be elusive -- but go ahead, give it a try, surprise me.

--
Eric Sosman
(E-Mail Removed)lid
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-13-2010
"Datesfat Chicks" <(E-Mail Removed)> writes:
> """" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Hello,
>> I would like to implement array of bits using the appropriate data
>> type for the machine
>> in which the code is compiled. So, using 32 bits for 32 bits machines
>> (and os), and 64 bits
>> for 64 bits machines (and os).
>> How can I know which is the natural word size of the machine and do
>> all the rest?

>
> That depends on whether you want to do it at compile time or runtime.
>
> At compile time ... other posters have suggested various limit files.
>
> Also (and somebody will flame me for this--don't blame them), the standards
> may (or may not) require a correlation between the way the preprocessor and
> the compiler behave. So something like:
>
> #if ((1 << 32) == 0)
> ... this is a 32-bit platform
> #else
> ... this is a 64-bit platform
> #endif
>
> might (or might not) be required to work.


Why would anyone flame you for being unsure about this?

The actual requirement in C99 is that preprocessor arithmetic is
done in the equivalent of intmax_t and uintmax_t, so the above
won't do what you're trying to do. And since intmax_t must be at
least 64 bits, 1<<32 will never be 0 in the preprocessor. Even if
that weren't the case, shifting a 32-bit value by 32 or more bits
is undefined.

> As far as at runtime ... the standard technique is to keep left-shifting "1"
> as an unsigned integer and count how many times it takes to become 0, i.e.
>
> unsigned how_many_bits(void)
> {
> unsigned mask=1, count=0;
>
> while(mask)
> {
> mask <<= 1;
> count++;
> }
>
> return(count);
> }


This is much simpler:

#include <limits.h>
sizeof(int) * CHAR_BIT

Your function is likely to give a more useful answer if unsigned
int has padding bits, but I don't know of any implementation where
it does.

> I'm gonna get so flamed ...


No, just corrected. (Some people can't tell the difference; I
trust you can.)

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Ersek, Laszlo
Guest
Posts: n/a
 
      05-13-2010
On Thu, 13 May 2010, Eric Sosman wrote:

> On 5/13/2010 3:48 PM, "" wrote:
>> Yes, I read the FAQ.
>>
>> My doubts are not how to do operations, but how can I choose
>> the most appropriate type for bitset.
>> I.e. choose a 32 bits type on a 32 bits machine,
>> and a 64 bits type on a 64 bits machine.

>
> If you can define precisely what you mean by "32 bits machine"
> and "64 bits machine," perhaps someone will think of a test you can
> make to tell them apart.


I believe the OP thinks of something like:

"64 bits machine": where uint64_t is supported, and the C compiler
generates machine instructions (or assembly code) that access such objects
as single machine words.

"32 bits machine": a machine -- which is not also a "64 bits machine" --
where uint32_t is supported, and where the C compiler generates machine
instructions (or assembly code) that access such objects as single machine
words.

I guess he's looking for the widest unsigned integer type that fits into a
machine register. Yes, I know. There is probably nothing in the standard
that would help us determine the answer portably. ("What's a register to
begin with?") The OP seems to ask for a portable guess that will work fast
on most sensible implemntations. I'd consider

#include <stdint.h> // SIZE_MAX
#include <limits.h> // UINT_MAX
#include <stddef.h> // size_t -- perhaps move this after the #if

#if SIZE_MAX > UINT_MAX
typedef size_t mytype;
#else
typedef unsigned mytype;
#endif

Cheers,
lacos
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shifting bits, shift 32 bits on 32 bit int GGG C++ 10 07-06-2006 06:09 AM
what about unsigned and signed 8 bits number, 16 bits, etc?? sarmin kho Python 2 06-15-2004 06:40 PM
8 bits/ch vs 16 bits/ch in PS Terry Digital Photography 5 01-21-2004 06:59 PM
8-Bits vs 12 or 16 bits/pixel; When does more than 8 bits count ? Al Dykes Digital Photography 3 12-29-2003 07:08 PM
win XP 32 bits on a 64 bits processor.. Abbyss Computer Support 3 11-13-2003 12:39 AM



Advertisments