1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Operations and constants for signed 8-bits integers (`i8` type)
13 #![allow(non_uppercase_statics)]
18 use from_str::FromStr;
19 use num::{Bitwise, Bounded, CheckedAdd, CheckedSub, CheckedMul};
20 use num::{CheckedDiv, Zero, One, strconv};
21 use num::{ToStrRadix, FromStrRadix};
22 use option::{Option, Some, None};
29 /// Returns the number of ones in the binary representation of the number.
31 fn count_ones(&self) -> i8 { unsafe { intrinsics::ctpop8(*self) } }
33 /// Returns the number of leading zeros in the in the binary representation
36 fn leading_zeros(&self) -> i8 { unsafe { intrinsics::ctlz8(*self) } }
38 /// Returns the number of trailing zeros in the in the binary representation
41 fn trailing_zeros(&self) -> i8 { unsafe { intrinsics::cttz8(*self) } }
44 impl CheckedAdd for i8 {
46 fn checked_add(&self, v: &i8) -> Option<i8> {
48 let (x, y) = intrinsics::i8_add_with_overflow(*self, *v);
49 if y { None } else { Some(x) }
54 impl CheckedSub for i8 {
56 fn checked_sub(&self, v: &i8) -> Option<i8> {
58 let (x, y) = intrinsics::i8_sub_with_overflow(*self, *v);
59 if y { None } else { Some(x) }
64 impl CheckedMul for i8 {
66 fn checked_mul(&self, v: &i8) -> Option<i8> {
68 let (x, y) = intrinsics::i8_mul_with_overflow(*self, *v);
69 if y { None } else { Some(x) }