From owner-freebsd-hackers@FreeBSD.ORG Wed Apr 4 08:38:05 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 78E6216A402 for ; Wed, 4 Apr 2007 08:38:05 +0000 (UTC) (envelope-from avg@icyb.net.ua) Received: from gateway.cybervisiontech.com.ua (gateway.cybervisiontech.com.ua [88.81.251.18]) by mx1.freebsd.org (Postfix) with ESMTP id 309BC13C480 for ; Wed, 4 Apr 2007 08:38:05 +0000 (UTC) (envelope-from avg@icyb.net.ua) Received: from localhost (hq.cybervisiontech.com [127.0.0.1]) by gateway.cybervisiontech.com.ua (Postfix) with ESMTP id 4CAACED54C4 for ; Wed, 4 Apr 2007 19:38:02 +0300 (EEST) X-Virus-Scanned: amavisd-new at cybervisiontech.com Received: from gateway.cybervisiontech.com.ua ([127.0.0.1]) by localhost (hq.cybervisiontech.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id mfbJTLd1xktJ for ; Wed, 4 Apr 2007 19:38:01 +0300 (EEST) Received: from [10.2.1.87] (rein.cybervisiontech.com.ua [10.2.1.87]) by gateway.cybervisiontech.com.ua (Postfix) with ESMTP id 6131AED54B0 for ; Wed, 4 Apr 2007 19:38:01 +0300 (EEST) Message-ID: <461363EA.1010604@icyb.net.ua> Date: Wed, 04 Apr 2007 11:38:02 +0300 From: Andriy Gapon User-Agent: Thunderbird 1.5.0.10 (X11/20070329) MIME-Version: 1.0 To: freebsd-hackers@freebsd.org References: <46128D47.50109@icyb.net.ua> <20070403180253.GG907@lizard.fafoe.narf.at> In-Reply-To: <20070403180253.GG907@lizard.fafoe.narf.at> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit 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: Wed, 04 Apr 2007 08:38:05 -0000 on 03/04/2007 21:02 Stefan Farfeleder said the following: > On Tue, Apr 03, 2007 at 08:22:15PM +0300, Andriy Gapon wrote: [...] >> $ ./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. Thank you for the pointer! I've been hacking some C for a while but haven't hit this thing nor theoretically learned it until now. Well, i think I'll have to add some checks/limits to x>>n constructs that I have now. Pity that it's impossible for a compiler to warn when right operand is a variable. -- Andriy Gapon