These are chat archives for rust-lang/rust

14th
Feb 2016
Robert Schütte
@Roba1993
Feb 14 2016 15:48
Hi guys, quick newbie question: I have a u8 and need to know of each bit if it's set or not. Did anyone have a good solution?
Jarred Nicholls
@jnicholls
Feb 14 2016 16:50
@Roba1993 bitwise AND using & operator...is that what you need?
Robert Schütte
@Roba1993
Feb 14 2016 17:30
@jnicholls Thanks for your response. I've done the following:
/// Checks if the bit at the requested position is set
fn get_bit_at(input: &u8, n: &u8) -> bool {
let input = input.clone() as i32;
    if n < &8 {
        input & (1 << n) != 0
    } else {
        false
    }
}
@jnicholls Seems to work..
Vladimir Matveev
@netvl
Feb 14 2016 17:55
@Roba1993 note that there is absolutely no reason to pass references to u8 into the function
u8 is trivially copyable
you won't need to call clone() as well if you pass arguments by value
Robert Schütte
@Roba1993
Feb 14 2016 18:51
@netvl should I never user references when im working with u8?
Jarred Nicholls
@jnicholls
Feb 14 2016 19:02
@Roba1993 Any primitive (Copy) type can just be moved and passed by value
Robert Schütte
@Roba1993
Feb 14 2016 19:03
okay thx, I get that wrong out of the documentation...
Robert Schütte
@Roba1993
Feb 14 2016 19:47
what is with arrays? are they also trivially copyable?
Jarred Nicholls
@jnicholls
Feb 14 2016 19:58
[T; 0] through [T; 32] are Copy where T: Copy
Robert Schütte
@Roba1993
Feb 14 2016 20:09
thx