On Fri, Aug 21, 2009 at 03:09:21AM +0400, Stanislav Maslovski wrote:
> On Fri, Aug 21, 2009 at 02:37:44AM +0400, Stanislav Maslovski wrote:
> > Чешем репу и вспоминаем, что сдвиги беззнаковых у нас циклические, а
> > не арифметические! С арифметическим сдвигом получится 0, как я и писал.
> 
> Про циклический сдвиг я, конечно, пробредил, сдвиг беззнаковых должен
> быть просто логическим. Но эффект налицо. Программисты на С, ау!

Хех. Давно я не брал в руки шашек. В качестве числа позиций сдвига
явно берется остаток от деления правого аргумента на разрядность
сдвигаемого числа (при сдвиге единицы влево это выглядит как
циклический сдвиг).

Чтобы не быть голословным, вот тест:

#include <stdio.h>
#include <sys/types.h>

main()
{
   u_int32_t n, m, p, q, r;
   int a, b;
   for (n = 0; n < 64; ++n)
   {
        m = ((u_int32_t)1) << n;
        p = ((u_int32_t)1) >> n;
        q = ((u_int32_t)1) << -n;
        r = ((u_int32_t)1) >> -n;
        a = n % 32;
        b = -n % 32;
        printf("n=%2d, a=%2d, m=%0.8x, p=%0.8x, b=%2d, q=%0.8x, r=%0.8x\n", n, 
a, m, p, b, q, r);
   }
}   

И его вывод:

n= 0, a= 0, m=00000001, p=00000001, b= 0, q=00000001, r=00000001
n= 1, a= 1, m=00000002, p=00000000, b=31, q=80000000, r=00000000
n= 2, a= 2, m=00000004, p=00000000, b=30, q=40000000, r=00000000
n= 3, a= 3, m=00000008, p=00000000, b=29, q=20000000, r=00000000
n= 4, a= 4, m=00000010, p=00000000, b=28, q=10000000, r=00000000
n= 5, a= 5, m=00000020, p=00000000, b=27, q=08000000, r=00000000
n= 6, a= 6, m=00000040, p=00000000, b=26, q=04000000, r=00000000
n= 7, a= 7, m=00000080, p=00000000, b=25, q=02000000, r=00000000
n= 8, a= 8, m=00000100, p=00000000, b=24, q=01000000, r=00000000
n= 9, a= 9, m=00000200, p=00000000, b=23, q=00800000, r=00000000
n=10, a=10, m=00000400, p=00000000, b=22, q=00400000, r=00000000
n=11, a=11, m=00000800, p=00000000, b=21, q=00200000, r=00000000
n=12, a=12, m=00001000, p=00000000, b=20, q=00100000, r=00000000
n=13, a=13, m=00002000, p=00000000, b=19, q=00080000, r=00000000
n=14, a=14, m=00004000, p=00000000, b=18, q=00040000, r=00000000
n=15, a=15, m=00008000, p=00000000, b=17, q=00020000, r=00000000
n=16, a=16, m=00010000, p=00000000, b=16, q=00010000, r=00000000
n=17, a=17, m=00020000, p=00000000, b=15, q=00008000, r=00000000
n=18, a=18, m=00040000, p=00000000, b=14, q=00004000, r=00000000
n=19, a=19, m=00080000, p=00000000, b=13, q=00002000, r=00000000
n=20, a=20, m=00100000, p=00000000, b=12, q=00001000, r=00000000
n=21, a=21, m=00200000, p=00000000, b=11, q=00000800, r=00000000
n=22, a=22, m=00400000, p=00000000, b=10, q=00000400, r=00000000
n=23, a=23, m=00800000, p=00000000, b= 9, q=00000200, r=00000000
n=24, a=24, m=01000000, p=00000000, b= 8, q=00000100, r=00000000
n=25, a=25, m=02000000, p=00000000, b= 7, q=00000080, r=00000000
n=26, a=26, m=04000000, p=00000000, b= 6, q=00000040, r=00000000
n=27, a=27, m=08000000, p=00000000, b= 5, q=00000020, r=00000000
n=28, a=28, m=10000000, p=00000000, b= 4, q=00000010, r=00000000
n=29, a=29, m=20000000, p=00000000, b= 3, q=00000008, r=00000000
n=30, a=30, m=40000000, p=00000000, b= 2, q=00000004, r=00000000
n=31, a=31, m=80000000, p=00000000, b= 1, q=00000002, r=00000000
n=32, a= 0, m=00000001, p=00000001, b= 0, q=00000001, r=00000001
n=33, a= 1, m=00000002, p=00000000, b=31, q=80000000, r=00000000
n=34, a= 2, m=00000004, p=00000000, b=30, q=40000000, r=00000000
n=35, a= 3, m=00000008, p=00000000, b=29, q=20000000, r=00000000
n=36, a= 4, m=00000010, p=00000000, b=28, q=10000000, r=00000000
n=37, a= 5, m=00000020, p=00000000, b=27, q=08000000, r=00000000
n=38, a= 6, m=00000040, p=00000000, b=26, q=04000000, r=00000000
n=39, a= 7, m=00000080, p=00000000, b=25, q=02000000, r=00000000
n=40, a= 8, m=00000100, p=00000000, b=24, q=01000000, r=00000000
n=41, a= 9, m=00000200, p=00000000, b=23, q=00800000, r=00000000
n=42, a=10, m=00000400, p=00000000, b=22, q=00400000, r=00000000
n=43, a=11, m=00000800, p=00000000, b=21, q=00200000, r=00000000
n=44, a=12, m=00001000, p=00000000, b=20, q=00100000, r=00000000
n=45, a=13, m=00002000, p=00000000, b=19, q=00080000, r=00000000
n=46, a=14, m=00004000, p=00000000, b=18, q=00040000, r=00000000
n=47, a=15, m=00008000, p=00000000, b=17, q=00020000, r=00000000
n=48, a=16, m=00010000, p=00000000, b=16, q=00010000, r=00000000
n=49, a=17, m=00020000, p=00000000, b=15, q=00008000, r=00000000
n=50, a=18, m=00040000, p=00000000, b=14, q=00004000, r=00000000
n=51, a=19, m=00080000, p=00000000, b=13, q=00002000, r=00000000
n=52, a=20, m=00100000, p=00000000, b=12, q=00001000, r=00000000
n=53, a=21, m=00200000, p=00000000, b=11, q=00000800, r=00000000
n=54, a=22, m=00400000, p=00000000, b=10, q=00000400, r=00000000
n=55, a=23, m=00800000, p=00000000, b= 9, q=00000200, r=00000000
n=56, a=24, m=01000000, p=00000000, b= 8, q=00000100, r=00000000
n=57, a=25, m=02000000, p=00000000, b= 7, q=00000080, r=00000000
n=58, a=26, m=04000000, p=00000000, b= 6, q=00000040, r=00000000
n=59, a=27, m=08000000, p=00000000, b= 5, q=00000020, r=00000000
n=60, a=28, m=10000000, p=00000000, b= 4, q=00000010, r=00000000
n=61, a=29, m=20000000, p=00000000, b= 3, q=00000008, r=00000000
n=62, a=30, m=40000000, p=00000000, b= 2, q=00000004, r=00000000
n=63, a=31, m=80000000, p=00000000, b= 1, q=00000002, r=00000000

-- 
Stanislav


-- 
To UNSUBSCRIBE, email to debian-russian-requ...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org

Ответить