From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 27167 invoked by alias); 6 Feb 2018 15:50:16 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 27109 invoked by uid 89); 6 Feb 2018 15:50:15 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_PASS,T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy=PROMOTIONS, balanced, admitted, imagination X-HELO: smtp02.uc3m.es Received: from smtp02.uc3m.es (HELO smtp02.uc3m.es) (163.117.176.132) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 06 Feb 2018 15:50:13 +0000 Received: from smtp02.uc3m.es (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 55FC84055; Tue, 6 Feb 2018 16:50:10 +0100 (CET) Received: from smtp02.uc3m.es (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 481D6404C; Tue, 6 Feb 2018 16:50:10 +0100 (CET) Received: from triangulo.it.uc3m.es (unknown [163.117.139.109]) by smtp02.uc3m.es (Postfix) with ESMTPS; Tue, 6 Feb 2018 16:50:10 +0100 (CET) Received: from nbd.it.uc3m.es (root@nbd.it.uc3m.es [163.117.139.192]) by triangulo.it.uc3m.es (8.13.4/8.13.4/Debian-3sarge3) with ESMTP id w16Fo9cE006943 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NO); Tue, 6 Feb 2018 16:50:10 +0100 Received: from nbd.it.uc3m.es (localhost [127.0.0.1]) by nbd.it.uc3m.es (8.13.1/8.13.1/Debian-15) with ESMTP id w16Fo951015179; Tue, 6 Feb 2018 16:50:09 +0100 Received: (from ptb@localhost) by nbd.it.uc3m.es (8.13.1/8.13.1/Submit) id w16Fo9lV015177; Tue, 6 Feb 2018 16:50:09 +0100 From: "Peter T. Breuer" Message-Id: <201802061550.w16Fo9lV015177@nbd.it.uc3m.es> Subject: Re: signed/unsigned integer conversion for right shift seems To: lh_mouse@126.com (Liu Hao) Date: Tue, 06 Feb 2018 15:50:00 -0000 Cc: Peter.T.Breuer@gmail.com, gcc-help@gcc.gnu.org In-Reply-To: <5a4c7714.12d45.1616bb64adf.Coremail.lh_mouse@126.com> from "Liu Hao" at Feb 06, 2018 11:22:44 PM X-Anonymously-To: Reply-To: ptb@inv.it.uc3m.es X-WebTV-Stationery: Standard\; BGColor=black\; TextColor=black Reply-By: Sat, 1 Apr 2006 14:21:08 -0700 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-TM-AS-GCONF: 00 X-imss-scan-details: No--14.583-5.0-31-10 X-TMASE-Version: IMSVA-9.1.0.1689-8.2.1013-23646.000 X-TMASE-Result: 10--14.582700-10.000000 X-TMASE-MatchedRID: vbSD0OnL8/Iv2k3M27J31szF4zZ+F8HuC/ExpXrHizwGW3hFnC9N1cxF Qxp3PhHyCyaRo+rOkvn89WDKQGB2LibkA69Y/24Ej56aIafJ4NHRahuPwaQ1Wg2G3vz8l/IE4Kc SW5ZFMivp2qE7bsoHKn9WIHCcJtfFXs3m3CwSaHLARsWbAyLXM0Crr/LkAQ46HFSQk97VYGqY7t hyNgdDnqHUXibxVTUFV+/3oguq0wJ211r83OOoVo9Ha73XaFhE+C/rJCmDGRsg/IN5/tyAcZQ+b aRiHI9R75fcmiKA9cqhRxQDQdChVWDSz0Ilzxe8aJiWzzHYz3TYYaSQU9SorWHaXGR9wawUeZ37 blWRCzN7h0LoFtVHAdFjaXLnlba+FwX9vEFWN2QzxdO2BTO204fGLZ++QpQzzT6yNk2P6imjxYy RBa/qJX3mXSdV7KK4QiE5bJj38wALbigRnpKlKSPzRlrdFGDw+9s6+69SF1NhLKMHzMo79XR/FI aQYs6SGX1uRNPArjAcTJVBnmAC1w== X-TMASE-SNAP-Result: 1.821001.0001-0-1-12:0,22:0,33:0,34:0-0 X-SW-Source: 2018-02/txt/msg00024.txt.bz2 "Also sprach Liu Hao:" > Well you could have declared the `printf()` function: Admitted, m'lud. But that would have meant an extra line of code, and I am miserly. > extern int printf(const char *restrict format, ...); > > > % ./a.out > > 0xe0000001 > > ^ SIGNED right shift > > > > OK, so x>>y was done signed. That means both x,y were converted to > > signed int (trivially). > > No. See below. I do not agree with the point made below, ingenious though it seems to me, hinging (apparently) on the meaning of "the". But let us see ... > > First: NO INTEGER PROMOTION is done, by this rule > > > > any operand whose type ranks lower than int is automatically converted > > to the type int, provided int is capable of representing all values of > > the operand's original type. If int is not sufficient, the operand is > > converted to unsigned int. > > > > Both x and y are int, so do not rank "lower than int". Rule does not > > apply. All as is. So INTEGER CONVERSION applies from there: > > It doesn't. > > I don't have C99 at hand, so I will quote C11 (WG14 N1570) for you: > > ``` > 6.5.7 Bitwise shift operators > ... > Semantics > 3 The integer promotions are performed on each of the operands. ... > ``` > > Note that the standard references _the integer promotions_, in contrast > to what you see elsewhere: I am afraid that "the" may refer to something that does not exist but is properly identified, such as "the large white rabbit that I met yesterday between the third and fourth pubs". It's a definite article, not a claim of cardinality at least one. That interpretation is helped by the "S" on the end of "promotionS", because in the case of an empty set of things, we still refer to the things in the empty set as being thingS [whereas I grant you it would be odd to refer to the thing (singular) in the empty set]. In other words, the plural allows for 0,1,2,... number of promotions to be applied. I can find no rule that applies a promotion, so I contend the answer is 0. The horses that almost ran me over yesterday are a figment of my imagination. > 6.5.5 Multiplicative operators > ... > Semantics > 3 The usual arithmetic conversions are performed on the operands. Those are conversions, not promotions. I aver that zero PROMOTIONS and one CONVERSION (to UNSIGNED int) should be applied by the rule I quoted. > ``` > 6.5.9 Equality operators > ... > Semantics > 4 If both of the operands have arithmetic type, the usual arithmetic > conversions are performed. Ditto. > Clearly, the type of the operand on the right-hand side is not altered - > it remains `unsigned int`. Yes. And arithmetic operations are applied to operands of the same type. That's what conversion is for: The usual arithmetic conversions are rules that provide a mechanism to yield a common type when both operands of a binary operator are ^^^^^^^^^^^^^^ balanced to a common type ... Ergo, one was unsigned, so they should both have been. (I'm willing to entertain ingenious interpretations of the word "type" or "common" if that is what you are planning next). Regards PTB