From owner-freebsd-chat@FreeBSD.ORG Wed Feb 13 01:43:43 2008 Return-Path: Delivered-To: freebsd-chat@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id B7EB816A419 for ; Wed, 13 Feb 2008 01:43:43 +0000 (UTC) (envelope-from deeptech71@gmail.com) Received: from fg-out-1718.google.com (fg-out-1718.google.com [72.14.220.154]) by mx1.freebsd.org (Postfix) with ESMTP id 4D50813C478 for ; Wed, 13 Feb 2008 01:43:42 +0000 (UTC) (envelope-from deeptech71@gmail.com) Received: by fg-out-1718.google.com with SMTP id 16so4469084fgg.35 for ; Tue, 12 Feb 2008 17:43:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:user-agent:mime-version:to:subject:content-type:content-transfer-encoding; bh=/p5y263f5wszR4WgwzET5FWoQX2OAurJQ5/u+H9HFx0=; b=vAhe3bipP+kaGRrTMUAnq01LwkP4bgo8QvlpucdpsEliBbfYyDl3VkfQumQ0ASPzpkMX+vcSK5CTcMwdPoJBsfnGace9cK5whW7VLOefCqyGFotx7GG6d2LhP4zVIrHsKC0jouULsggIKgrDlKlgbb8N2ETk12essUnq1r/OZMw= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject:content-type:content-transfer-encoding; b=jJSOhE3o9qzK16WPGQnILTwAwRGMT7QzgGwx1BL0PHJi6ReQG/0BZduc9j7vhABrBdk5JJfOK4I4r1placUqnag7DhverknlLZ24ybrwAwnFp1YqTlCssSqLusW96sBorxdc+5oSYYKFJjtx3s5AQ2NVKyGfcbb3CA+c7I1QLRc= Received: by 10.86.95.20 with SMTP id s20mr1878106fgb.6.1202865335611; Tue, 12 Feb 2008 17:15:35 -0800 (PST) Received: from ?192.168.123.1? ( [84.0.109.233]) by mx.google.com with ESMTPS id d6sm1131334fga.9.2008.02.12.17.15.32 (version=SSLv3 cipher=RC4-MD5); Tue, 12 Feb 2008 17:15:33 -0800 (PST) Message-ID: <47B24480.9020001@gmail.com> Date: Wed, 13 Feb 2008 02:14:40 +0100 From: deeptech71@gmail.com User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.11) Gecko/20071128 SeaMonkey/1.1.7 MIME-Version: 1.0 To: freebsd-chat@freebsd.org Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: bits wrap when leftshifting non-constant amounts X-BeenThere: freebsd-chat@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Non technical items related to the community List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 13 Feb 2008 01:43:43 -0000 My gcc 3.4.6 behaves weirdly when left shifting, and I couldn't find any info on this. the program: #include int main( void ) { unsigned n; for( n = 13; n < 100; n += 7 ) { printf( "0x42f1u << %u = %u\n", n, 0x42f1u << n ); } return 0; } the output: 0x42f1u << 13 = 140386304 0x42f1u << 20 = 789577728 0x42f1u << 27 = 2281701376 0x42f1u << 34 = 68548 0x42f1u << 41 = 8774144 0x42f1u << 48 = 1123090432 0x42f1u << 55 = 2021654528 0x42f1u << 62 = 1073741824 0x42f1u << 69 = 548384 0x42f1u << 76 = 70193152 0x42f1u << 83 = 394788864 0x42f1u << 90 = 3288334336 0x42f1u << 97 = 34274 When I left shift a constant amount, it works. That is: ( 1u << 34u ) == 0 But using a variable: unsigned lsh = 34; ( 1u << lsh ) == 4 !!! It seems that lsh is first moduloed with the width of int. What the hell?