Rust bit splitting

ab: u32

            let mask = ((1 << 16 ) - 1) as u32;
            let a = ((ab >> 16) & mask) as u16;
            let b = ((ab >> 0) & mask) as u16;

Is there a cleaner way to split a u32 into two u16's? (I know that the &mask is redundant in the 'a' case, and the ">>0" is redundant in the b case, this is just to make it look a bit more symmetrical).

Since numeric casts to smaller types truncate you don't need the mask at all

let a = (ab >> 16) as u16;
let b = ab as u16;
3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.