473,686 Members | 2,182 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 1650
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
3953
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
2239
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
5883
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
2353
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
4878
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
1470
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
3011
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
1760
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
8585
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
8517
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
9057
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
8934
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...
1
8770
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
8780
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...
1
6441
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5800
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();...
2
2208
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.