Skip to content

Instantly share code, notes, and snippets.

@jdmichaud
Created February 16, 2017 21:05
Show Gist options
  • Save jdmichaud/c1f908c0e1e0251c3be0382d02c9ed1e to your computer and use it in GitHub Desktop.
Save jdmichaud/c1f908c0e1e0251c3be0382d02c9ed1e to your computer and use it in GitHub Desktop.
fn main() {
let i: u64 = 0xDEADBEEFCAFEBABE;
let b1: u8 = ( i & 0x00000000000000FF) as u8;
let b2: u8 = ((i >> 8) & 0x00000000000000FF) as u8;
let b3: u8 = ((i >> 16) & 0x00000000000000FF) as u8;
let b4: u8 = ((i >> 24) & 0x00000000000000FF) as u8;
let b5: u8 = ((i >> 32) & 0x00000000000000FF) as u8;
let b6: u8 = ((i >> 40) & 0x00000000000000FF) as u8;
let b7: u8 = ((i >> 48) & 0x00000000000000FF) as u8;
let b8: u8 = ((i >> 56) & 0x00000000000000FF) as u8;
println!("b1 {}", format!("{:x}", b1));
println!("b2 {}", format!("{:x}", b2));
println!("b3 {}", format!("{:x}", b3));
println!("b4 {}", format!("{:x}", b4));
println!("b5 {}", format!("{:x}", b5));
println!("b6 {}", format!("{:x}", b6));
println!("b7 {}", format!("{:x}", b7));
println!("b8 {}", format!("{:x}", b8));
}
@jdmichaud
Copy link
Author

Output on x86:

b1 be
b2 ba
b3 fe
b4 ca
b5 ef
b6 be
b7 ad
b8 de

@jdmichaud
Copy link
Author

                 DE AD BE EF    DE AD BE EF
address 0 DE <---^  |  |  |      |  |  |  ^---> EF address 0
address 1 AD <------   |  |      |  |   ------> BE address 1
address 2 BE <---------   |      |   ---------> AD address 2
address 3 EF <------------        ------------> DE address 3

           Big-Endian                    Little-Endian

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment