From owner-freebsd-hackers@FreeBSD.ORG Tue Apr 3 18:19:53 2007 Return-Path: X-Original-To: freebsd-hackers@freebsd.org Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 4BCF116A401 for ; Tue, 3 Apr 2007 18:19:53 +0000 (UTC) (envelope-from stefan@fafoe.narf.at) Received: from viefep15-int.chello.at (viefep13-int.chello.at [213.46.255.15]) by mx1.freebsd.org (Postfix) with ESMTP id 7C2E213C4B9 for ; Tue, 3 Apr 2007 18:19:51 +0000 (UTC) (envelope-from stefan@fafoe.narf.at) Received: from lizard.fafoe.narf.at ([213.47.85.26]) by viefep16-int.chello.at (InterMail vM.6.01.05.04 201-2131-123-105-20051025) with ESMTP id <20070403180254.YEWJ27364.viefep16-int.chello.at@lizard.fafoe.narf.at>; Tue, 3 Apr 2007 20:02:54 +0200 Received: by lizard.fafoe.narf.at (Postfix, from userid 1001) id DB4AAB8BB; Tue, 3 Apr 2007 20:02:53 +0200 (CEST) Date: Tue, 3 Apr 2007 20:02:53 +0200 From: Stefan Farfeleder To: Andriy Gapon Message-ID: <20070403180253.GG907@lizard.fafoe.narf.at> Mail-Followup-To: Andriy Gapon , freebsd-hackers@freebsd.org References: <46128D47.50109@icyb.net.ua> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <46128D47.50109@icyb.net.ua> User-Agent: Mutt/1.5.14 (2007-02-12) Cc: freebsd-hackers@freebsd.org Subject: Re: strange bit-shifting X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 03 Apr 2007 18:19:53 -0000 On Tue, Apr 03, 2007 at 08:22:15PM +0300, Andriy Gapon wrote: > > $ cat test_shl.c > #include > #include > > int main() > { > uint64_t l; > > l = 0; > l--; > printf("%.16lX\n", l); > l <<= 64; > printf("%.16lX\n", l); > return 0; > } > > $ cc test_shl.c -o test_shl > test_shl.c: In function `main': > test_shl.c:11: warning: left shift count >= width of type > $ ./test_shl > FFFFFFFFFFFFFFFF > FFFFFFFFFFFFFFFF > $ uname -srm > FreeBSD 6.2-RELEASE-p2 amd64 > $ gcc -v > Using built-in specs. > Configured with: FreeBSD/amd64 system compiler > Thread model: posix > gcc version 3.4.6 [FreeBSD] 20060305 > > What gives ? It looks like shift is actually done not by specified > number of bits but by that number modulo 64. > Please also mind that the same thing happens if I use a variable instead > of a constant in that expression. The behaviour is undefined and you even got a warning from GCC. C99 6.5.7: # The integer promotions are performed on each of the operands. The type # of the result is that of the promoted left operand. If the value of # the right operand is negative or is greater than or equal to the width # of the promoted left operand, the behavior is undefined. Stefan