mirror of
https://github.com/uutils/coreutils
synced 2024-12-18 09:03:14 +00:00
od: refactor sign_extend
easier to understand algoritm which does not use unsafe
This commit is contained in:
parent
e0b7ff1953
commit
3e143217a9
1 changed files with 2 additions and 8 deletions
10
src/od/od.rs
10
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::<u64,i64>(1u64 << (b - 1));
|
||||
let x = mem::transmute::<u64,i64>(item) & (mem::transmute::<u64,i64>(1u64 << b) - 1);
|
||||
let r = (x ^ m) - m;
|
||||
r
|
||||
}
|
||||
let shift = 64 - itembytes * 8;
|
||||
(item << shift) as i64 >> shift
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue