From 3e143217a9f093f048680cb60406d24b5c21c5f8 Mon Sep 17 00:00:00 2001 From: Wim Hueskes Date: Thu, 21 Jul 2016 10:20:42 +0200 Subject: [PATCH] od: refactor sign_extend easier to understand algoritm which does not use unsafe --- src/od/od.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/od/od.rs b/src/od/od.rs index f09cb8134..39a09964d 100644 --- a/src/od/od.rs +++ b/src/od/od.rs @@ -424,14 +424,8 @@ fn print_item_hex(p: u64, itembytes: usize) { fn sign_extend(item: u64, itembytes: usize) -> i64{ - // https://graphics.stanford.edu/~seander/bithacks.html#VariableSignExtend - unsafe{ - let b = 8 * itembytes; // number of bits representing the number in p - let m = mem::transmute::(1u64 << (b - 1)); - let x = mem::transmute::(item) & (mem::transmute::(1u64 << b) - 1); - let r = (x ^ m) - m; - r - } + let shift = 64 - itembytes * 8; + (item << shift) as i64 >> shift }