473,766 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Value Bits Vs Object Bits


The quantity of bits used by an unsigned integer type in memory can be
determined by:
typedef unsigned long long UIntType;

CHAR_BIT * sizeof(UIntType )
However, what would be a good portable way to determine how many of these
bits are value bits? If possible, it would be nice to have it as a
compile time constant.
Here's something I played around with:
typedef unsigned long long UIntType;
unsigned GetQuantityValu eBits(void)
{
/* We know it's atleast 8 in anyway */

unsigned quantity = 8;

UIntType val = 128;

while (val <<= 1) ++quantity;

return quantity;
}
Also, we could determine the amount of non-value bits (trapping bits
perhaps) from:

sizeof(UIntType ) * CHAR_BIT - GetQuantityValu eBits()
-Tomás
Jun 2 '06 #1
13 2086
Tomás wrote:
The quantity of bits used by an unsigned integer type in memory can be
determined by:
typedef unsigned long long UIntType;

CHAR_BIT * sizeof(UIntType )
However, what would be a good portable way to determine how many of these
bits are value bits? If possible, it would be nice to have it as a
compile time constant.


Speaking for myself, I've never been able to discover a
way to write a constant expression for this. However, it's
easy enough to determine at run-time by starting with a value
of all-ones and counting how many one-bit shifts are required
before the value becomes zero. You could also try something
like ceil(log((UIntT ype)-1)/log(2)).

It is occasionally annoying that this information is not
easily available. For many uses an upper bound is all that's
needed and CHAR_BIT*sizeof (UIntType) is good enough. For a
different class of uses a range bound is wanted, and you can
get that from (UIntType)-1. But for things like managing
"arrays" of single-bit flags packed into larger units, it is
annoying that the value bits can't be counted at compile time.

Confession: I usually ignore the possibility of P.B.s and
just pretend CHAR_BIT*sizeof is the Right Answer. The nasal
demons haven't punished me yet (for that sin, anyway), but of
course the Standard makes no guarantee about the timeliness
of retribution.

--
Eric Sosman
es*****@acm-dot-org.invalid
Jun 2 '06 #2
Eric Sosman writes:
Tomás wrote:
The quantity of bits used by an unsigned integer type in memory can be
determined by:
typedef unsigned long long UIntType;
CHAR_BIT * sizeof(UIntType )
However, what would be a good portable way to determine how many of
these bits are value bits? If possible, it would be nice to have it as
a compile time constant.
Speaking for myself, I've never been able to discover a
way to write a constant expression for this.


I have. Posted it before.

/* Number of bits in inttype_MAX, or in any (1<<k)-1 where 0 <= k < 3.2E+10 */
#define IMAX_BITS(m) ((m) /((m)%0x3fffffff L+1) /0x3fffffffL %0x3fffffffL *30 \
+ (m)%0x3fffffffL /((m)%31+1)/31%31*5 + 4-12/((m)%31+3))

So IMAX_BITS((unsi gned_type)-1) computes the number of bits in an
unsigned integer type. IMAX_BITS(INT_M AX) computes the number of bits
in an int. Until someone implements a 4-gigabyte integer type:-)

Or if you are less paranoid about how large UINTMAX_MAX can get:

/* Number of bits in inttype_MAX, or in any (1<<k)-1 where 0 <= k < 2040 */
#define IMAX_BITS(m) ((m)/((m)%255+1) / 255%255*8 + 7-86/((m)%255+12))
Confession: I usually ignore the possibility of P.B.s and
just pretend CHAR_BIT*sizeof is the Right Answer. The nasal
demons haven't punished me yet (for that sin, anyway), but of
course the Standard makes no guarantee about the timeliness
of retribution.


As long as that just wastes a bit of memory when it's wrong, that's my
preference too. IMAX_BITS is a fun little hack, but not exactly
readable.

Explanation, were 'x**y' means x raised to the power of y:
Line 1 computes (number of whole 30-bit chunks) * 30:
For m = (2**(K*n+r))-1 and P = (2**K)-1 with K=30, P=0x3fffffff,
m = (P+1)**n * 2**r - 1,
m % P + 1 = 1 * 2**r - 1 + 1 = 2**r when 2**r-1<P so r<K,
m /(m%P+1) = (P+1)**n - 1
= ((P+1)-1) * ((P+1)**0 +...+ (P+1)**(n-1)),
.../P%P*K = ( 1**0 +...+ 1**(n-1)) % P * K
= n*K when n < P.
Part 2 does the same to the remainder (m%0x3fffffff) with K=5, P=31.
Part 3 "happens" to count the final 0-4 bits in m%31=[0/1/3/7/15].
m % 31 is short for m % 0x3fffffff % 31, because 0x3fffffff % 31 = 0.
0x3fffffffL is the largest portable 2**x-1 with such a 2**y-1 factor.

--
Hallvard
Jun 2 '06 #3
Hallvard B Furuseth wrote:
/* Number of bits in inttype_MAX, or in any (1<<k)-1 where 0 <= k < 3.2E+10 */
#define IMAX_BITS(m) ((m) /((m)%0x3fffffff L+1) /0x3fffffffL %0x3fffffffL *30 \
+ (m)%0x3fffffffL /((m)%31+1)/31%31*5 + 4-12/((m)%31+3))

So IMAX_BITS((unsi gned_type)-1) computes the number of bits in an
unsigned integer type.


Great job. Thanks!

--
Thad
Jun 3 '06 #4
Hallvard B Furuseth wrote:
Eric Sosman writes:
Tomás wrote:

The quantity of bits used by an unsigned integer type in memory can be
determined by:
typedef unsigned long long UIntType;
CHAR_BIT * sizeof(UIntType )
However, what would be a good portable way to determine how many of
these bits are value bits? If possible, it would be nice to have it as
a compile time constant.


Speaking for myself, I've never been able to discover a
way to write a constant expression for this.

I have. Posted it before.

/* Number of bits in inttype_MAX, or in any (1<<k)-1 where 0 <= k < 3.2E+10 */
#define IMAX_BITS(m) ((m) /((m)%0x3fffffff L+1) /0x3fffffffL %0x3fffffffL *30 \
+ (m)%0x3fffffffL /((m)%31+1)/31%31*5 + 4-12/((m)%31+3))


Yikes! It looks like an IOCCC snippet, but ... I am in awe.

--
Eric Sosman
es*****@acm-dot-org.invalid
Jun 3 '06 #5
Some time ago I wrote:
/* Number of bits in inttype_MAX, or in any (1<<k)-1 where 0 <= k < 3.2E+10 */
#define IMAX_BITS(m) ((m) /((m)%0x3fffffff L+1) /0x3fffffffL %0x3fffffffL *30 \
+ (m)%0x3fffffffL /((m)%31+1)/31%31*5 + 4-12/((m)%31+3))

So IMAX_BITS((unsi gned_type)-1) computes the number of bits in an
unsigned integer type. IMAX_BITS(INT_M AX) computes the number of bits
in an int. Until someone implements a 4-gigabyte integer type:-)


Eh. An int has IMAX_BITS(INT_M AX)+1 bits - with the sign bit.

--
Hallvard
Jun 18 '06 #6
Hallvard B Furuseth posted:
#define IMAX_BITS(m) ((m) /((m)%0x3fffffff L+1) /0x3fffffffL
%0x3fffffffL *30 \
+ (m)%0x3fffffffL /((m)%31+1)/31%31*5 +
4-12/((m)%31+3))

Are you sure that works? It seems to work a lot of the time... but
sometimes I get inaccurate values.
If I enter 2048 into the following program, I get 112 bits:
#define IMAX_BITS(m) \
((m) /((m)%0x3fffffff L+1) /0x3fffffffL %0x3fffffffL *30 \
+ (m)%0x3fffffffL /((m)%31+1)/31%31*5 + 4-12/((m)%31+3))
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
for(;;)
{
printf( "\nEnter a number: " );

unsigned long val;

scanf( "%lu", &val );

if ( 999 == val ) break;

printf("\n\nIt consists of: %lu bits\n\n", IMAX_BITS(val) );

system("PAUSE") ;
}
}

--

Frederick Gotham
Jun 25 '06 #7
Frederick Gotham wrote:

Hallvard B Furuseth posted:
#define IMAX_BITS(m) ((m) /((m)%0x3fffffff L+1) /0x3fffffffL
%0x3fffffffL *30 \
+ (m)%0x3fffffffL /((m)%31+1)/31%31*5 +
4-12/((m)%31+3))


Are you sure that works? It seems to work a lot of the time... but
sometimes I get inaccurate values.

If I enter 2048 into the following program, I get 112 bits:


Me too.
There are also problems with higher numbers.

--
pete
Jun 25 '06 #8
Frederick Gotham wrote:
Hallvard B Furuseth posted:

#define IMAX_BITS(m) ((m) /((m)%0x3fffffff L+1) /0x3fffffffL
%0x3fffffff L *30 \
+ (m)%0x3fffffffL /((m)%31+1)/31%31*5 +
4-12/((m)%31+3))

Are you sure that works? It seems to work a lot of the time... but
sometimes I get inaccurate values.
If I enter 2048 into the following program, I get 112 bits:
[code snipped; it just passes input numbers to the macro]


Somewhere along the line, somebody has snipped away the
comment that preceded the macro when originally posted:
/* Number of bits in inttype_MAX, or in any (1<<k)-1
where 0 <= k < 3.2E+10 */


That is, the macro is only advertised to work for arguments
that are one less than a power of two. It is not a general-
purpose compile-time log() function!

--
Eric Sosman
es*****@acm-dot-org.invalid
Jun 25 '06 #9
Eric Sosman posted:

Somewhere along the line, somebody has snipped away the
comment that preceded the macro when originally posted:
/* Number of bits in inttype_MAX, or in any (1<<k)-1
where 0 <= k < 3.2E+10 */


That is, the macro is only advertised to work for arguments
that are one less than a power of two. It is not a general-
purpose compile-time log() function!

Hehe... I found the code so confusing that I didn't pay attention to the
comments.

"One less than a power of two" would refer to numbers where every bit is
one, e.g.

111

11111111

11111

111111111111
Anyhow, the macro is a stroke of genius. I wonder what other gems
Hallvard B Furuseth has come up with... ?
A compile time "Log base 2" macro would be brilliant...
--

Frederick Gotham
Jun 25 '06 #10

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

Similar topics

36
28122
by: Riccardo Rossi | last post by:
Hi all! How does Python pass arguments to a function? By value or by reference? Thanks, Riccardo Rossi.
8
3081
by: DaKoadMunky | last post by:
Please consider the following... <CODE> #include <string> using namespace std; typedef int PrimitiveType; typedef string ClassType;
30
1584
by: Alf P. Steinbach | last post by:
The C++ FAQ item 29.5 (this seems to be strongly related to C), at <url: http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.5> mentions that <quote> C++ guarantees a char is exactly one byte which is at least 8 bits, short is at least 16 bits, int is at least 16 bits, and long is at least 32 bits. </quote>
8
9072
by: Tom | last post by:
Here is what I do to get a single value from my database (using Oracle ODP as example): Dim ID as Object Dim cmdTest as New OracleCommand("select ID from MyTable where key = " & KeySearch") ID=cmdText.ExecuteNonQuery If ID is Nothing then 'Not found - do not found processing here Else
35
12580
by: Frederick Gotham | last post by:
I'm writing a template, and I need to know the maximum value of a given integer type at compile time. something like: template<class NumType> class Arb { public: static NumType const max = /* maximum value */; };
7
6603
by: gene kelley | last post by:
I have an application where I need to read some header information found in 3 different types of file types. Two of the file types were fairly straight forward as the items to read in the header are at least 8 bits (one byte), so, I'm able to step through a file stream with a binary reader and retrive the data. The last file type, however, has me stumped at the moment. The header spec specifies the item lengths in bits. Most of the...
14
1770
by: KK | last post by:
Dear All I have a small problem with using as operator on value type array. Here is an example what I am trying to do. using System; using System.Collections.Generic; using System.Text;
42
3552
by: polas | last post by:
Afternoon all, I have some code (which I thought was OK, but now appreciate is probably not by the standard) which contains int a; ...... if (a==NULL) { ....
275
12379
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
9568
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...
1
9959
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9837
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
8833
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5279
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...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3929
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
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.