Can anyone explain why '>>2' shift means 'divided by 4' in C codes?

后端 未结 10 1496
谎友^
谎友^ 2020-12-05 09:25

I know and understand the result.

For example:


7 (decimal) = 00000111 (binary)
and 7 >> 2 = 00000001 (binary)
10条回答
  •  执念已碎
    2020-12-05 09:42

    Elaborating on Aniket Inge's answer:

    Number: 30710 = 1001100112

    How multiply by 10 works in decimal system

    10 * (30710)

    = 10 * (3*102 + 7*100)

    = 3*102+1 + 7*100+1

    = 3*103 + 7*101

    = 307010

    = 30710 << 1

    Similarly multiply by 2 in binary,

    2 * (1001100112)

    = 2 * (1*28 + 1*25 + 1*24 + 1*21 1*20)

    = 1*28+1 + 1*25+1 + 1*24+1 + 1*21+1 1*20+1

    = 1*29 + 1*26 + 1*25 + 1*22 + 1*21

    = 10011001102

    = 1001100112 << 1

提交回复
热议问题