operator of shift <<

Kind time!
As a result of implementation of the code there has to be a result - 001100 or 1100.
But I receive 1100110000.
What do I do not so?

int a;
void setup() {
Serial.begin(19200);
a=0b11001100;
}

void loop() {
delay (1000);
Serial.println(a<<2, BIN);
}

byte a;

Serial.println((byte)(a<<2), BIN);

a = 0b11001100;

a = a & 0b00001111;

a becomes 1100

A type int is 16-bits. :wink: Does that help you?

or 1100

Given what you started with (zeros in the two rightmost bits) you can't get that after shifting left two bits. You're going to end-up with 4 zeros in the 4 rightmost bits.

int a;
a=0b11001100;
Serial.println(a<<2, BIN); 
}

'a' is a 16-bit value. When you set it to 0b11001100 you get:

0b0000000011001100
shifted left two bits you get:
0b0000001100110000

There are lots of ways to get 0000000000001100:
You could do 'a >> 4' instead of 'a << 2'.
You could mask away all but the bottom 4 to 6 bits:
a & 0b0000000000001111
a & 0b0000000000011111
a & 0b0000000000111111

What made you think that shifting left two bits was what you wanted to do?

johnwasser:

int a;

a=0b11001100;
Serial.println(a<<2, BIN);
}




'a' is a 16-bit value. When you set it to 0b11001100 you get:

0b0000000011001100
shifted left two bits you get:
0b0000001100110000

Everything is right, this 16-bit value. But if I shift 8 bits, then as a result I receive additional zero, and values do not move.
The most interesting that in a week earlier everything worked))
I could solve a problem having replaced int with uint16_t.

ybpvin:
Everything is right, this 16-bit value. But if I shift 8 bits, then as a result I receive additional zero, and values do not move.

I don't understand what you mean by "if I shift 8 bits, then as a result I receive additional zero, and values do not move". Can you show a small sketch that demonstrates the problem?

johnwasser:
I don't understand what you mean by "if I shift 8 bits, then as a result I receive additional zero, and values do not move". Can you show a small sketch that demonstrates the problem?

I replaced the type "a" by 8 bits.

byte a=0b11010011;

void setup() {
Serial.begin(19200);
}

void loop() {
Serial.println (a<<2, BIN);
delay (500);
}

Result

1101001100
1101001100
1101001100
1101001100
1101001100
1101001100

The output looks correct to me. What did you expect it to be ?

UKHeliBob:
The output looks correct to me. What did you expect it to be ?

UKHeliBob:
The output looks correct to me. What did you expect it to be ?

Bit variable 8! The result should not be 10 bits.
Or I am mistaken?

ybpvin:
Bit variable 8! The result should not be 10 bits.

Says who?

The result of an expression is printed, not the value of a variable.
Shift the variable and it will behave as expected.

a <<= 2;

ybpvin:
Bit variable 8! The result should not be 10 bits.
Or I am mistaken?

You are mistaken.

Bitwise shift operators:
The return type is the type of the left operand after integral promotions.

The following implicit conversions are classified as integral promotions:

signed char or signed short can be converted to int;

unsigned char, char8_t (since C++20) or unsigned short can be converted to int if it can hold its entire value range, and unsigned int otherwise;

johnwasser:
You are mistaken.

unsigned char, char8_t (since C++20) or unsigned short can be converted to int if it can hold its entire value range, and unsigned int otherwise;

Many thanks for the help.