operator

Rust <<: shift bits left

Left shift moves integer bits toward higher positions, inserts zeros on the right and discards bits beyond the fixed width. Expected output (not executed): 00001000 8 Why: The lowest bit moves three positions to the left. Common mistake: A shift count at least as large as the type width, or negative, is invalid under overflow checking. Do not assume shifting preserves all bits. Advanced connection: `std::ops::Shl`; `ShlAssign` for `<<=`.

Formal Syntax & Structure

rust syntaxrust
a << n

Working Example

Example Snippetrust
fn main() {    let flag = 1_u8 << 3;    println!("{flag:08b}");    println!("{flag}");}
Rust Reference