0
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
    printf("left shift 1 = %d\n", 1 << 1);
    printf("left shift 2 = %d\n", 1 << 2);
    printf("left shift 3 = %d\n", 1 << 3);
    printf("left shift 4 = %d\n", 1 << 4);
    printf("left shift 5 = %d\n", 1 << 5);
    printf("left shift 6 = %d\n", 1 << 6);
    printf("left shift 7 = %d\n", 1 << 7);

    return 0;
}

and I got the output like this:

left shift 1 = 2
left shift 2 = 4
left shift 3 = 8
left shift 4 = 16
left shift 5 = 32
left shift 6 = 64
left shift 7 = 128

It seem correct for number 1 and 2, but what happened to other numbers from 3 to 7?

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
Leo_Wang
  • 67
  • 6
  • 3
    Isn't each one twice the previous, as expected? – Weather Vane Apr 19 '20 at 09:56
  • 3
    "what happened to other numbers from 3 to 7?" - nothing out of ordinary, they're correct. How are they "strange", in your opinion? – ForceBru Apr 19 '20 at 09:57
  • Those are correct numbers. If you were expecting something different, you don't understand what left-shift does. – WhozCraig Apr 19 '20 at 09:57
  • Each left shift increases the value to the next *Power of Two*. – David C. Rankin Apr 19 '20 at 09:58
  • 1
    Are you mixing up operands? In all cases it's 1 left shifted by operand on the right of << operator, not the other way around. – JohnLM Apr 19 '20 at 10:03
  • oh , I just found out that 1 << 3 , means left shift 1 to 3 bits. But I just mistakend that 1 << 3 means left shift 3 by 1 bit, sorry about that . So it is all right. – Leo_Wang Apr 19 '20 at 10:05

3 Answers3

1

There's nothing strange about that at all. For an unsigned integer (also usually for a signed integer where you don't encroach on the sign bit, but this is not mandated by the standard), a left shift is basically a doubling of the value.

And that's exactly what you're seeing:

1 << 0 = 0000 0001 =   1
1 << 1 = 0000 0010 =   2
1 << 2 = 0000 0100 =   4
1 << 3 = 0000 1000 =   8
1 << 4 = 0001 0000 =  16
1 << 5 = 0010 0000 =  32
1 << 6 = 0100 0000 =  64
1 << 7 = 1000 0000 = 128
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

The left shift operation n << 1 in fact multiplies the left operand by 2. And the result of your program shows this.

Lets assume that you have an object named n of the type char

0000 0001

then n << 1 gives

0000 0010 

that in the decimal notation is 2

n << 2 gives

0000 0100

that in the decimal notation is 4

n << 3 gives

0000 1000

that is the decimal notation is 8.

And so on.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • "in fact multiplies the left operand by 2" it does that *by definition*. It's not a mere useful fact. – Aykhan Hagverdili Apr 19 '20 at 10:33
  • 1
    @Ayxan You are wrong. Processors shift instructions are not the same as multiplication instructions. They are different instructions. – Vlad from Moscow Apr 19 '20 at 10:34
  • Look up C18 §6.5.7/4. Definition of `E1 << E2` is `E1 × 2 ^ E2`. – Aykhan Hagverdili Apr 19 '20 at 10:46
  • @Ayxan In the C11 Standard there is written "4 The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are filled with zeros. If E1 has an unsigned type, the value of the result is E1 × 2E2..." The same is written in the C 20. – Vlad from Moscow Apr 19 '20 at 10:50
  • Yes, the value of the result is `E1 × 2 ^ E2` even if you have a 1's complement representation, or even a decimal machine – Aykhan Hagverdili Apr 19 '20 at 10:58
  • Just a minor clarification: the left shift being the same as doubling is only guaranteed for *unsigned* types, of which `int` is not one. – paxdiablo Apr 19 '20 at 14:33
0

What you experience is the correct behavior of the shifting operator.

Let's start from illustrating the decimal representation of a byte:

ByteDecValue = 
    bit0 * 2^0 +
    bit1 * 2^1 +
    bit2 * 2^2 +
    bit3 * 2^3 +
    bit4 * 2^4 +
    bit5 * 2^5 +
    bit6 * 2^6 +
    bit7 * 2^7

So, for example, the decimal value corresponding to the byte 00010010b is 2^4 + 2^1 = 18 dec.

An interesting particular scenario occurs when only one byte is 1. Deriving the formula from the one above we can say

ByteDecValue_bit_N = 2^N

2^0 = 1 << 0 = 0000 0001
2^1 = 1 << 1 = 0000 0010
2^2 = 1 << 2 = 0000 0100
2^3 = 1 << 3 = 0000 1000
2^4 = 1 << 4 = 0001 0000
2^5 = 1 << 5 = 0010 0000
2^6 = 1 << 6 = 0100 0000
2^7 = 1 << 7 = 1000 0000

That's exactly what you experience: by shifting 1 to the left N times you get a single 1 in position N, so the power 2^N.

Note: your test would have been complete by printing also the value of 1 << 0. In that case you would have got 2^0 = 1. No shift at all means keeping the original value.

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39