Skip to content

Commit 9fadb48

Browse files
committed
vari32 vari64 fix (i hope)
1 parent 755a71a commit 9fadb48

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

src/lib.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,13 +397,20 @@ pub mod binary {
397397
#[inline]
398398
pub fn get_var_i32(&mut self) -> i32 {
399399
let raw = self.get_var_u32();
400-
((raw >> 1) as i32) ^ -((raw & 1) as i32)
400+
let mut value = (raw >> 1) as i32;
401+
if raw & 1 != 0 {
402+
value = !value;
403+
}
404+
value
401405
}
402406

403407
/// Writes a signed variable-length integer (ZigZag encoded).
404408
#[inline]
405409
pub fn put_var_i32(&mut self, value: i32) {
406-
let encoded = ((value << 1) ^ (value >> 31)) as u32;
410+
let mut encoded = (value << 1) as u32;
411+
if value < 0 {
412+
encoded = !encoded;
413+
}
407414
self.put_var_u32(encoded);
408415
}
409416

@@ -437,13 +444,20 @@ pub mod binary {
437444
#[inline]
438445
pub fn get_var_i64(&mut self) -> i64 {
439446
let raw = self.get_var_u64();
440-
((raw >> 1) as i64) ^ (-((raw & 1) as i64))
447+
let mut value = (raw >> 1) as i64;
448+
if raw & 1 != 0 {
449+
value = !value;
450+
}
451+
value
441452
}
442453

443454
/// Writes a signed 64-bit variable-length integer (ZigZag encoded).
444455
#[inline]
445456
pub fn put_var_i64(&mut self, value: i64) {
446-
let encoded = ((value << 1) ^ (value >> 63)) as u64;
457+
let mut encoded = (value << 1) as u64;
458+
if value < 0 {
459+
encoded = !encoded;
460+
}
447461
self.put_var_u64(encoded);
448462
}
449463
}

0 commit comments

Comments
 (0)