gcc is clearly returning -1 when I do this
~ (unsigned short) 0
which is not the case if "unsigned int" is used instead of "unsigned
short".
What does the C standard say about that? (reference to the standard
is appreciated.)
Thanks.
Reza. 15 1524
RezaRob wrote:
>
gcc is clearly returning -1 when I do this
~ (unsigned short) 0
which is not the case if "unsigned int" is used instead of "unsigned
short".
What does the C standard say about that? (reference to the standard
is appreciated.)
If INT_MAX is greater than or equal to USHRT_MAX,
then (~ (unsigned short) 0) means the same thing as
(~ (int)(unsigned short) 0).
ISO/IEC 9899: 1990
6.2.1 Arithmetic operands
6.2.1.1 Characters and integers
A char, a short int, or an int bit-field,
or their signed or unsigned varieties,
or an enumeration type, may be used in an expression
wherever an int or unsigned int may be used.
If an int can represent all values of the original type,
the value is converted to an int; otherwise,
it is converted to an unsigned int.
These are called the integral romotions.
All other arithmetic types are unchanged by the integral promotions.
--
pete
pete wrote:
>
RezaRob wrote:
gcc is clearly returning -1 when I do this
~ (unsigned short) 0
which is not the case if "unsigned int" is used instead of "unsigned
short".
What does the C standard say about that? (reference to the standard
is appreciated.)
If INT_MAX is greater than or equal to USHRT_MAX,
then (~ (unsigned short) 0) means the same thing as
(~ (int)(unsigned short) 0).
ISO/IEC 9899: 1990
6.2.1 Arithmetic operands
6.2.1.1 Characters and integers
A char, a short int, or an int bit-field,
or their signed or unsigned varieties,
or an enumeration type, may be used in an expression
wherever an int or unsigned int may be used.
If an int can represent all values of the original type,
the value is converted to an int; otherwise,
it is converted to an unsigned int.
These are called the integral romotions.
That should be "These are called the integral promotions."
All other arithmetic types are unchanged by the integral promotions.
--
pete
On Sep 23, 3:47 pm, pete <pfil...@mindspring.comwrote:
If INT_MAX is greater than or equal to USHRT_MAX,
then (~ (unsigned short) 0) means the same thing as
(~ (int)(unsigned short) 0).
ISO/IEC 9899: 1990
6.2.1 Arithmetic operands
6.2.1.1 Characters and integers
[...]
Pete, I really appreciate this.
Reza.
RezaRob wrote:
>
On Sep 23, 3:47 pm, pete <pfil...@mindspring.comwrote:
If INT_MAX is greater than or equal to USHRT_MAX,
then (~ (unsigned short) 0) means the same thing as
(~ (int)(unsigned short) 0).
ISO/IEC 9899: 1990
6.2.1 Arithmetic operands
6.2.1.1 Characters and integers
[...]
Pete, I really appreciate this.
The following three expressions have the same type and value:
(USHRT_MAX)
((unsigned short) ~0u)
((unsigned short) -1)
--
pete
The following three expressions have the same type and value:
(USHRT_MAX)
((unsigned short) ~0u)
((unsigned short) -1)
Are you sure about the middle one?
1: 0u is of type unsigned int.
2: ~0u is of type unsigned int and is equal to UINT_MAX.
3: (unsigned short)~0u will be UINT_MAX % USHRT_MAX, or at least I
think it will, so it can't be USHRT_MAX.
but of course I'm open to correction!
Martin
On Sep 24, 4:36 pm, Martin Wells <war...@eircom.netwrote:
The following three expressions have the same type and value:
(USHRT_MAX)
((unsigned short) ~0u)
((unsigned short) -1)
Are you sure about the middle one?
Sure, there's a cast in it to unsigned short.
;-)
[snip]
Martin Wells said:
>
>The following three expressions have the same type and value: (USHRT_MAX) ((unsigned short) ~0u) ((unsigned short) -1)
Are you sure about the middle one?
1: 0u is of type unsigned int.
2: ~0u is of type unsigned int and is equal to UINT_MAX.
3: (unsigned short)~0u will be UINT_MAX % USHRT_MAX, or at least I
think it will, so it can't be USHRT_MAX.
but of course I'm open to correction!
Oh good. :-) Re your Point 3, a single counter-example should suffice.
Consider a system with UINT_MAX == USHRT_MAX (e.g. typical 1990s MS-DOS
systems, or an SIL64 system such as at least one Cray. Clearly, on such a
system, UINT_MAX % USHRT_MAX will be 0 (for the same reason that x % x is
0), and yet (unsigned short)~0u will be equivalent to ~0u, which is most
emphatically NOT 0.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Richard:
Oh good. :-) Re your Point 3, a single counter-example should suffice.
Consider a system with UINT_MAX == USHRT_MAX (e.g. typical 1990s MS-DOS
systems, or an SIL64 system such as at least one Cray. Clearly, on such a
system, UINT_MAX % USHRT_MAX will be 0 (for the same reason that x % x is
0), and yet (unsigned short)~0u will be equivalent to ~0u, which is most
emphatically NOT 0.
Sorry I've red that a few times but I still don't understand what
you're saying.
Was pete right or wrong?
Martin
Martin Wells said:
Richard:
>Oh good. :-) Re your Point 3, a single counter-example should suffice. Consider a system with UINT_MAX == USHRT_MAX (e.g. typical 1990s MS-DOS systems, or an SIL64 system such as at least one Cray. Clearly, on such a system, UINT_MAX % USHRT_MAX will be 0 (for the same reason that x % x is 0), and yet (unsigned short)~0u will be equivalent to ~0u, which is most emphatically NOT 0.
Sorry I've red that a few times but I still don't understand what
you're saying.
You claimed that "(unsigned short)~0u will be UINT_MAX % USHRT_MAX". I
disputed that claim, by pointing out a counter-example.
Was pete right or wrong?
Presumably. :-)
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Richard:
Was pete right or wrong?
Presumably. :-)
I'm still lost. I'll try think it through using MS-DOS as an example:
1: 0u == the number 0
2: ~0u == UINT_MAX == the number 65535
3: (unsigned short)~0u == the number USHRT_MAX % 65535 == the
number 65535 % 65535 == the number 0.
This leads me to believe that pete was wrong... unless I'm missing
something.
Martin
Martin Wells said:
Richard:
Was pete right or wrong?
Presumably. :-)
I'm still lost.
<snip>
3: (unsigned short)~0u == the number USHRT_MAX % 65535 == the
number 65535 % 65535 == the number 0.
Please explain why you think this is the case. I can see no justification
for your assumption that (unsigned short)~0u is equal to USHRT_MAX % 65535
(and yes, I know we're talking 16-bit systems. You originally wrote that
it's equal to UINT_MAX % USHRT_MAX, which is also incorrect.
You might reasonably claim that, on a 16-bit system, (unsigned short)~0u is
equal to UINT_MAX % (USHRT_MAX + 1), but that would just be equal to
UINT_MAX (and indeed USHRT_MAX).
This leads me to believe that pete was wrong... unless I'm missing
something.
You appear to be missing a + 1. :-)
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Martin Wells wrote:
>
The following three expressions have the same type and value:
(USHRT_MAX)
((unsigned short) ~0u)
((unsigned short) -1)
Are you sure about the middle one?
1: 0u is of type unsigned int.
2: ~0u is of type unsigned int and is equal to UINT_MAX.
3: (unsigned short)~0u will be UINT_MAX % USHRT_MAX, or at least I
think it will, so it can't be USHRT_MAX.
but of course I'm open to correction!
This part of your post: "UINT_MAX % USHRT_MAX"
is wrong.
--
pete
pete:
The following three expressions have the same type and value:
(USHRT_MAX)
((unsigned short) ~0u)
((unsigned short) -1)
I'm gonna give the middle one another go. . .
Before I begin though, I'm gonna pretend I'm working with the
following machine:
CHAR_BIT == 11
sizeof(short) == 2 (all 22 bits are value bits)
sizeof(int) == 3 (31 bits are value bits, 1 is padding)
Therefore, we have:
UINT_MAX = 2147483647
USHRT_MAX = 4194303
1: 0u
Expression Type: unsigned int
Expression Value: 0
2: ~0u
Expression Type: unsigned int
Expression Value: 2147483647 (i.e. UINT_MAX)
3: (unsigned short)~0u
== UINT_MAX % (USHRT_MAX + 1)
== 2147483647 % 4194304
== 4194303
Oh, so that worked out OK. I suppose what I was tryna get my head
around from the start was how you could so blindy make the assumption
Given: a is a positive integer
b is a positive integer
a is greater than or equal to b
That:
( pow(2,a) - 1 ) % pow(2,b)
is equal to:
pow(2,b) - 1
Martin
sizeof(int) == 3 (31 bits are value bits, 1 is padding)
Shuda written "2 are padding" instead of "1 is".
Martin
Martin Wells wrote:
>
pete:
The following three expressions have the same type and value:
(USHRT_MAX)
((unsigned short) ~0u)
((unsigned short) -1)
I'm gonna give the middle one another go. . .
Before I begin though, I'm gonna pretend I'm working with the
following machine:
CHAR_BIT == 11
sizeof(short) == 2 (all 22 bits are value bits)
sizeof(int) == 3 (31 bits are value bits, 1 is padding)
Therefore, we have:
UINT_MAX = 2147483647
USHRT_MAX = 4194303
1: 0u
Expression Type: unsigned int
Expression Value: 0
2: ~0u
Expression Type: unsigned int
Expression Value: 2147483647 (i.e. UINT_MAX)
3: (unsigned short)~0u
== UINT_MAX % (USHRT_MAX + 1)
== 2147483647 % 4194304
== 4194303
Oh, so that worked out OK.
Actually, I got one of those wrong.
The type of USHRT_MAX isn't unsigned short.
--
pete This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Roger Leigh |
last post by:
I've written a simple container template class to contain a single
value. This emits a signal when the value is changed (it's used as a
notifier of changes), and listeners can connect to its...
|
by: Piotr Wyderski |
last post by:
Hello,
is it possible to reuse a friend operator which is defined
inside a class? I'd like to obtain the following behaviour:
class integer {
integer operator +(signed long int v) const...
|
by: Martin Magnusson |
last post by:
I have defined a number of custom stream buffers with corresponding in
and out streams for IO operations in my program, such as IO::output,
IO::warning and IO::debug. Now, the debug stream should...
|
by: Dennis |
last post by:
I would like to dynamically allocate in a sub a 2 dimensional Array
float *myarray = new float ;
of course I get an error. How do you allocate a 2D array using the New
operator?
I...
|
by: Martin |
last post by:
Below is some sample code to illustrate two warnings I get using a Hitachi
compiler. When I use the GCC compiler I get a clean compile. I'd be grateful
if someone could explain what the warnings...
| |
by: Carlo Capelli |
last post by:
I found a change in the following code, that behaved
correctly in VC++ 6.
#include <strstream>
using namespace std;
void main()
{
char x;
ostrstream(x, 100) << "pippo" << "pluto" << ends;...
|
by: DanielJohnson |
last post by:
I am reading K&R and right now in section 2.9 bitwise operators. I
understood most of the simple things initially but getting stuck with
the later part where different functions like getbits(),...
|
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= |
last post by:
Operator overloads are just like any other member function, you
can make them do whatever you want. However, of course, we might
expect them to behave in a certain way. The ++ operator should...
|
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;
|
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,...
|
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...
| |
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,...
|
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...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |