473,703 Members | 5,073 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trap representations for unsigned integers

If uMyInt_t is an unsigned integral type, is the following a
necessary and sufficient condition that uMyInt_t has no trap
representation?

(uMyInt_t)(-1) >CHAR_BIT*sizeo f(uMyInt_t)-1

That is, I'm asking wheter it equals 0 whenever uMyInt_t has trap
representations , equals a nonzero value whenever uMyInt_t has no
trap representation, and never triggers undefined behaviour.

--
#include <stdio.h>
char s[]="\16Jsa ukenethr ,cto haCr\n";int main(void){*s*= 5;*
s%=23;putchar(s[0][s]);return*s-14?main():!putc har(9[s+*s]);}
Apr 21 '07 #1
17 1657
Army1987 wrote:
If uMyInt_t is an unsigned integral type, is the following a
necessary and sufficient condition that uMyInt_t has no trap
representation?

(uMyInt_t)(-1) >CHAR_BIT*sizeo f(uMyInt_t)-1
No. If uMyInt_t has padding bits, you will right-shift by a number
greater than (or equal to) the number of value bits, and for that the
behaviour is undefined.

Apr 21 '07 #2
Army1987 wrote:
If uMyInt_t is an unsigned integral type, is the following a
necessary and sufficient condition that uMyInt_t has no trap
representation?

(uMyInt_t)(-1) >CHAR_BIT*sizeo f(uMyInt_t)-1

That is, I'm asking wheter it equals 0 whenever uMyInt_t has trap
representations , equals a nonzero value whenever uMyInt_t has no
trap representation, and never triggers undefined behaviour.
I think there are at least two problems with this test.
First, if uMyInt_t has padding bits the shift count may be
too large and lead to undefined behavior ("may" because of
possible promotion to int or unsigned int). Second, the
presence of padding bits does not imply the existence of trap
representations : the extra bits may just be along for the ride.

The best way to detect padding bits may be to count the
number of 1's in (uMyInt_t)-1, or to compare the numeric value
of (uMyInt_t)-1 to the "expected" quantity. The first test is
easy at run time but difficult or impossible at preprocessing
time; the second has problems, too (what type should you use
to form the expected value?). I can think of no reliable way
to determine whether trap representations exist; if you find
there are no padding bits you can deduce that there are no
traps, but that's as far as I think you can get.

--
Eric Sosman
es*****@acm-dot-org.invalid
Apr 21 '07 #3
"Harald van D?k" <tr*****@gmail. comha scritto nel messaggio
news:11******** **************@ b58g2000hsg.goo glegroups.com.. .
Army1987 wrote:
>If uMyInt_t is an unsigned integral type, is the following a
necessary and sufficient condition that uMyInt_t has no trap
representation ?

(uMyInt_t)(-1) >CHAR_BIT*sizeo f(uMyInt_t)-1

No. If uMyInt_t has padding bits, you will right-shift by a number
greater than (or equal to) the number of value bits, and for that the
behaviour is undefined.
Is

( DBL_MAX >= (uMyInt_t)(-1) || (puts("Your DS9K is about to self-\
destruct. Get a real computer."), exit(1)),
/* On the DeathStation 9000 exit(1) activates self-destruction */
ceil(log2((uMyI nt_t)(-1))) >= CHAR_BIT*sizeof (uMyInt_t) )

any better?
Apr 21 '07 #4
Army1987 wrote:
"Harald van D?k" <tr*****@gmail. comha scritto nel messaggio
news:11******** **************@ b58g2000hsg.goo glegroups.com.. .
Army1987 wrote:
If uMyInt_t is an unsigned integral type, is the following a
necessary and sufficient condition that uMyInt_t has no trap
representation?

(uMyInt_t)(-1) >CHAR_BIT*sizeo f(uMyInt_t)-1
No. If uMyInt_t has padding bits, you will right-shift by a number
greater than (or equal to) the number of value bits, and for that the
behaviour is undefined.

Is

( DBL_MAX >= (uMyInt_t)(-1) || (puts("Your DS9K is about to self-\
destruct. Get a real computer."), exit(1)),
/* On the DeathStation 9000 exit(1) activates self-destruction */
ceil(log2((uMyI nt_t)(-1))) >= CHAR_BIT*sizeof (uMyInt_t) )

any better?
On the DS9K, DBL_MAX would be large enough, but CHAR_BIT *
sizeof(uMuInt_t ) would give the wrong result because SIZE_MAX is too
small. :)

Seriously though, your current expression is already no longer an
integer constant expression, so at this point there's no downside to
just writing a function.

Apr 21 '07 #5
"Eric Sosman" <es*****@acm-dot-org.invalidha scritto nel messaggio
news:Cc******** *************** *******@comcast .com...
Army1987 wrote:
>If uMyInt_t is an unsigned integral type, is the following a
necessary and sufficient condition that uMyInt_t has no trap
representation ?

(uMyInt_t)(-1) >CHAR_BIT*sizeo f(uMyInt_t)-1

That is, I'm asking wheter it equals 0 whenever uMyInt_t has trap
representation s, equals a nonzero value whenever uMyInt_t has no
trap representation, and never triggers undefined behaviour.
[snip] Second, the
presence of padding bits does not imply the existence of trap
representations : the extra bits may just be along for the ride.
So I'll replace "necessary and sufficient condition" with
"sufficient condition".

What I was thinking of is something like:

#include <string.h>
unsigned char randchar();
/* Get a random integer from 0 to UCHAR_MAX */

unsigned long longrand()
{
unsigned long result;
if (NO_TRAP(unsign ed long)) {
int i;
unsigned char res[sizeof result];
for (i=0; i<sizeof result; i++)
res[i] = randchar();
memcpy(&result, res, sizeof result);
} else {
/* invent something else */
}
return result;
}

I would still be able to use the algorithm with the right result (a
uniformly distributed random integer from 0 to UINT_MAX) if there
are padding bits but they are ignored.
Apr 21 '07 #6
"Harald van D?k" <tr*****@gmail. comha scritto nel messaggio
news:11******** **************@ l77g2000hsb.goo glegroups.com.. .
Army1987 wrote:
>Is

( DBL_MAX >= (uMyInt_t)(-1) || (puts("Your DS9K is about to self-\
destruct. Get a real computer."), exit(1)),
/* On the DeathStation 9000 exit(1) activates self-destruction */
ceil(log2((uMy Int_t)(-1))) >= CHAR_BIT*sizeof (uMyInt_t) )

any better?

On the DS9K, DBL_MAX would be large enough, but CHAR_BIT *
sizeof(uMuInt_t ) would give the wrong result because SIZE_MAX is too
small. :)
On C99 I might use (uintmax_t)CHAR _BIT*sizeof(uMy Int_t)...

Seriously though, your current expression is already no longer an
integer constant expression, so at this point there's no downside to
just writing a function.
I was thinking of using a macro, so I could write
NO_PADDING(size _t), NO_PADDING(unsi gned int), etc. There's no way
to do that with a function.
Apr 21 '07 #7
Army1987 wrote:
"Eric Sosman" <es*****@acm-dot-org.invalidha scritto nel messaggio
news:Cc******** *************** *******@comcast .com...
Army1987 wrote:
If uMyInt_t is an unsigned integral type, is the following a
necessary and sufficient condition that uMyInt_t has no trap
representation?

(uMyInt_t)(-1) >CHAR_BIT*sizeo f(uMyInt_t)-1

That is, I'm asking wheter it equals 0 whenever uMyInt_t has trap
representations , equals a nonzero value whenever uMyInt_t has no
trap representation, and never triggers undefined behaviour.

[snip] Second, the
presence of padding bits does not imply the existence of trap
representations : the extra bits may just be along for the ride.

So I'll replace "necessary and sufficient condition" with
"sufficient condition".

What I was thinking of is something like:

#include <string.h>
unsigned char randchar();
/* Get a random integer from 0 to UCHAR_MAX */

unsigned long longrand()
{
unsigned long result;
if (NO_TRAP(unsign ed long)) {
int i;
unsigned char res[sizeof result];
for (i=0; i<sizeof result; i++)
res[i] = randchar();
memcpy(&result, res, sizeof result);
} else {
/* invent something else */
}
return result;
}

I would still be able to use the algorithm with the right result (a
uniformly distributed random integer from 0 to UINT_MAX) if there
are padding bits but they are ignored.
Why not

unsigned long result = randchar();
if ((unsigned long) -1 UCHAR_MAX)
{
size_t i;
for (i = sizeof result - 1; i != 0; i--)
result = (result << CHAR_BIT) | randchar();
}

It works regardless of any padding bits.

Apr 21 '07 #8
Army1987 wrote:
"Harald van D?k" <tr*****@gmail. comha scritto nel messaggio
news:11******** **************@ l77g2000hsb.goo glegroups.com.. .
Army1987 wrote:
Is

( DBL_MAX >= (uMyInt_t)(-1) || (puts("Your DS9K is about to self-\
destruct. Get a real computer."), exit(1)),
/* On the DeathStation 9000 exit(1) activates self-destruction */
ceil(log2((uMyI nt_t)(-1))) >= CHAR_BIT*sizeof (uMyInt_t) )

any better?
On the DS9K, DBL_MAX would be large enough, but CHAR_BIT *
sizeof(uMuInt_t ) would give the wrong result because SIZE_MAX is too
small. :)
On C99 I might use (uintmax_t)CHAR _BIT*sizeof(uMy Int_t)...
And on C90, you can use a cast to unsigned long.
Seriously though, your current expression is already no longer an
integer constant expression, so at this point there's no downside to
just writing a function.

I was thinking of using a macro, so I could write
NO_PADDING(size _t), NO_PADDING(unsi gned int), etc. There's no way
to do that with a function.
You could do

#define NO_PADDING(type ) (count_bits((ty pe) -1) == sizeof(type))

where count_bits accepts an unsigned long / uintmax_t.

Apr 21 '07 #9
"Harald van D?k" <tr*****@gmail. comha scritto nel messaggio
news:11******** **************@ e65g2000hsc.goo glegroups.com.. .
Why not

unsigned long result = randchar();
if ((unsigned long) -1 UCHAR_MAX)
{
size_t i;
for (i = sizeof result - 1; i != 0; i--)
result = (result << CHAR_BIT) | randchar();
}

It works regardless of any padding bits.
I hadn't thought of that before... (Mathematically x << y << z << t
is equivalent to x << (y+z+t), but I hadn't thought to the fact
that the former might be valid C even when the latter may be UB.)
Thanks.
Apr 21 '07 #10

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

Similar topics

96
3956
by: John Harrison | last post by:
I knew that unsigned integral data types were the cause of scads of mostly spurious warning messages, but I didn't realise that they were a security risk too (see here http://www.securitytracker.com/alerts/2004/Feb/1009067.html). All for one measly extra bit. So has the time come for C++ to deprecate unsigned integral types? john
8
2240
by: Rade | last post by:
Following a discussion on another thread here... I have tried to understand what is actually standardized in C++ regarding the representing of integers (signed and unsigned) and their conversions. The reference should be 3.9.1 (Fundamental types), and 4.7 (Integral conversions). It seems to me that the Standard doesn't specify: 1) The "value representation" of any of these types, except that (3.9.1/3) "... The range of nonnegative...
49
5888
by: Neil Zanella | last post by:
Hello, Often I happen to be dealing with nonnegative integers and since I know I won't need negative numbers here I declare them as unsigned simply to make the program somewhat clearer. Effectively, though, signed integers would often work too since unless I am doing some modular arithmetic modulo the word length then I almost never need to use the high bit since the integers I deal with are usually not that large, and I would assume...
11
437
by: pemo | last post by:
Ambiguous? I have a student who's asked me to explain the following std text (esp. the footnote). 6.2.6.1.5 Certain object representations need not represent a value of the object type. If the stored value of an object has such a representation and is read by an lvalue
10
2354
by: pemo | last post by:
As far as I understand it, a trap representation means something like - an uninitialised automatic variable might /implicitly/ hold a bit-pattern that, if read, *might* cause a 'trap' (I'm not sure what 'a trap' means here - anyone?). I also read into this that, an *initialised* automatic variable, may never hold a bit pattern that might, when read, cause a 'trap'. I.e., if an auto is explicitly initialised, it may *never* hold a...
24
4885
by: hammer1234 | last post by:
Hi I am wondering if there is a way of using the underlying bit representations of floats. I am interested in creating a violation in MISRA C:2004 of rule "The underlying bit representations shall not be used" I have treid using bitwise operators but these are not compilable. Is there a way I can use bit representations of floats?
24
1471
by: Lane Straatman | last post by:
I'm trying to think of what one-third looks like when represented by two's complement. What is its length? I'm not schur. It would be a DWORD in assembly. In c, it would be (I think:) float r = (float)(one third) My syntax is somewhat wanting. I think the question can be stated unambiguously if I ask what one-third looks like with two's complement representation and a length of 32 bits. -- LS
10
3014
by: Richard Tobin | last post by:
May all-bits-zero be a trap representation for a pointer? What if I calloc() space for a structure containing pointers? -- Richard -- "Consideration shall be given to the need for as many as 32 characters in some alphabets" - X3.4, 1963.
11
1761
by: vippstar | last post by:
Hello Let's assume CHAR_BIT == 8, sizeof (int) == 4 and INT_MAX == 32767 (that'd mean int has 32 bits, of which only the 15 are value bits) Now, my question is, are the value bits required to be contiguous in the representation? Is the position of the signed bit fixed (ie, the last bit, the first bit)
0
8760
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
9254
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...
1
9017
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
8967
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
7872
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
4433
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
4687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2458
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2070
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.