Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > ~ operator returns signed value

Reply
Thread Tools

~ operator returns signed value

 
 
Martin Wells
Guest
Posts: n/a
 
      09-25-2007
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

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      09-25-2007
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
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      09-25-2007
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
 
Reply With Quote
 
Martin Wells
Guest
Posts: n/a
 
      09-26-2007
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

 
Reply With Quote
 
Martin Wells
Guest
Posts: n/a
 
      09-26-2007

> sizeof(int) == 3 (31 bits are value bits, 1 is padding)



Shuda written "2 are padding" instead of "1 is".

Martin

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      09-26-2007
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
 
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
Convert a signed binary number into a signed one ? Rob1bureau VHDL 1 02-27-2010 12:13 AM
signed(12 downto 0) to signed (8 downto 0) kyrpa83 VHDL 1 10-17-2007 06:58 PM
createImage sometime returns null and sometime returns non-null. vizlab Java 3 10-17-2007 11:21 AM
New window.name value doesn't stick. Old value returns unless the window is refreshed Diana Javascript 1 04-06-2004 12:09 AM
warning - comparing a signed value to an unsinged value Kevin Goodsell C Programming 30 10-22-2003 12:12 PM



Advertisments