This question already has answers here:
What are the exact semantics of Rust's shift operators?
(2 answers)
Closed 1 year ago.
How to make Unsigned right shift (>>>) in rust? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Unsigned_right_shift
For positive numbers you can just use the shift operator >>.
For negative numbers however, I'm going to assume you're dealing with an i32. The behavior in the docs you linked shifts the binary representation of a negative number to the right. Therefore we first need to reinterpret the negative integer as an unsigned integer. The safest way of doing it would be with the use of to_be_bytes and from_be_bytes.
fn main() {
let a: u32 = 5;
let b: u32 = 2;
let c: i32 = -5;
let c_as_u32: u32 = {
let bytes = c.to_be_bytes();
u32::from_be_bytes(bytes)
};
let x = a >> b;
let y = c_as_u32 >> b;
println!("x = {}", x); // x = 1
println!("y = {}", y); // x = 1073741822
}
Related
I was trying to raise an integer to a power using the caret operator (^), but I am getting surprising results, e.g.:
assert_eq!(2^10, 8);
How can I perform exponentiation in Rust?
Rust provides exponentiation via methods pow and checked_pow. The latter
guards against overflows. Thus, to raise 2 to the power of 10, do:
let base: i32 = 2; // an explicit type is required
assert_eq!(base.pow(10), 1024);
The caret operator ^ is not used for exponentiation, it's the bitwise XOR
operator.
Here is the simplest method which you can use:
let a = 2; // Can also explicitly define type i.e. i32
let a = i32::pow(a, 10);
It will output "2 raised to the power of 10", i.e.:
1024
For integers:
fn main() {
let n = u32::pow(2, 10);
println!("{}", n == 1024);
}
For floats:
fn main() {
// example 1
let f = f32::powf(2.0, 10.0);
// example 2
let g = f32::powi(2.0, 10);
// print
println!("{}", f == 1024.0 && g == 1024.0);
}
or, since your base is 2, you can also use shift:
fn main() {
let n = 2 << 9;
println!("{}", n == 1024);
}
https://doc.rust-lang.org/std/primitive.f32.html#method.powf
https://doc.rust-lang.org/std/primitive.f32.html#method.powi
https://doc.rust-lang.org/std/primitive.u32.html#method.pow
I was trying the same thing as the OP. Thanks to the other answer authors.
Here's a variation that works for me:
let n = 2u32.pow(10);
This uses a literal unsigned 32 bit integer to set the type and base, then calls the pow() function on it.
Bit shifting is a good way to do this particular case:
assert_eq!(1 << 10, 1024);
I'm representing numbers as ratios of signed 64-bit integers using the num-rational crate's Rational64 type. I'm trying to round a number down to the next multiple of another number, and I'm getting integer overflow issues when I do it in either of the two obvious ways. Note that both of the numbers may be fractions.
Normalize to an integer
extern crate num_rational;
extern crate num_traits;
use num_rational::Rational64;
use num_traits::identities::Zero;
fn round(mut n: Rational64, increment: Rational64) -> Rational64 {
let rem = n % increment;
if !rem.is_zero() {
// normalize to a multiple of the increment, round down
// to the next integer, and then undo the normalization
n = (n * increment.recip()).trunc() * increment;
}
n
}
fn main() {
let a = Rational64::new(10_000_676_909_441, 8_872_044_800_000_000);
let b = Rational64::new(1, 1_000_000);
let c = round(a, b);
println!("{}", c);
}
(playground)
Subtract the remainder
extern crate num_rational;
extern crate num_traits;
use num_rational::Rational64;
use num_traits::identities::Zero;
fn round(mut n: Rational64, increment: Rational64) -> Rational64 {
let rem = n % increment;
if !rem.is_zero() {
n -= rem;
}
n
}
fn main() {
let a = Rational64::new(10_000_676_909_441, 8_872_044_800_000_000);
let b = Rational64::new(1, 1_000_000);
let c = round(a, b);
println!("{}", c);
}
(playground)
Is there a way to make it so that n is rounded down to a multiple of increment such that integer overflow is less likely? It's fine if I have to extract the numerator and denominator (both Rust i64 types) and do math on them directly.
I am trying to find the sum of the digits of a given number. For example, 134 will give 8.
My plan is to convert the number into a string using .to_string() and then use .chars() to iterate over the digits as characters. Then I want to convert every char in the iteration into an integer and add it to a variable. I want to get the final value of this variable.
I tried using the code below to convert a char into an integer:
fn main() {
let x = "123";
for y in x.chars() {
let z = y.parse::<i32>().unwrap();
println!("{}", z + 1);
}
}
(Playground)
But it results in this error:
error[E0599]: no method named `parse` found for type `char` in the current scope
--> src/main.rs:4:19
|
4 | let z = y.parse::<i32>().unwrap();
| ^^^^^
This code does exactly what I want to do, but first I have to convert each char into a string and then into an integer to then increment sum by z.
fn main() {
let mut sum = 0;
let x = 123;
let x = x.to_string();
for y in x.chars() {
// converting `y` to string and then to integer
let z = (y.to_string()).parse::<i32>().unwrap();
// incrementing `sum` by `z`
sum += z;
}
println!("{}", sum);
}
(Playground)
The method you need is char::to_digit. It converts char to a number it represents in the given radix.
You can also use Iterator::sum to calculate sum of a sequence conveniently:
fn main() {
const RADIX: u32 = 10;
let x = "134";
println!("{}", x.chars().map(|c| c.to_digit(RADIX).unwrap()).sum::<u32>());
}
my_char as u32 - '0' as u32
Now, there's a lot more to unpack about this answer.
It works because the ASCII (and thus UTF-8) encodings have the Arabic numerals 0-9 ordered in ascending order. You can get the scalar values and subtract them.
However, what should it do for values outside this range? What happens if you provide 'p'? It returns 64. What about '.'? This will panic. And '♥' will return 9781.
Strings are not just bags of bytes. They are UTF-8 encoded and you cannot just ignore that fact. Every char can hold any Unicode scalar value.
That's why strings are the wrong abstraction for the problem.
From an efficiency perspective, allocating a string seems inefficient. Rosetta Code has an example of using an iterator which only does numeric operations:
struct DigitIter(usize, usize);
impl Iterator for DigitIter {
type Item = usize;
fn next(&mut self) -> Option<Self::Item> {
if self.0 == 0 {
None
} else {
let ret = self.0 % self.1;
self.0 /= self.1;
Some(ret)
}
}
}
fn main() {
println!("{}", DigitIter(1234, 10).sum::<usize>());
}
If c is your character you can just write:
c as i32 - 0x30;
Test with:
let c:char = '2';
let n:i32 = c as i32 - 0x30;
println!("{}", n);
output:
2
NB: 0x30 is '0' in ASCII table, easy enough to remember!
Another way is to iterate over the characters of your string and convert and add them using fold.
fn sum_of_string(s: &str) -> u32 {
s.chars().fold(0, |acc, c| c.to_digit(10).unwrap_or(0) + acc)
}
fn main() {
let x = "123";
println!("{}", sum_of_string(x));
}
When using below function:
fn factors(number: &BigInt) -> Vec<BigInt> {
let mut n = number.clone();
let mut i: BigInt = ToBigInt::to_bigint(&2).unwrap();
let mut factors = Vec::<BigInt>::new();
while i * i <= n {
if (n % i) == ToBigInt::to_bigint(&1).unwrap() {
i = i + ToBigInt::to_bigint(&1).unwrap();
}
else {
n = n/i as BigInt;
factors.push(i);
}
i = i + ToBigInt::to_bigint(&1).unwrap();
}
if n > i {
factors.push(n);
}
factors
}
I get moved value errors for literally every time i or n is used, starting from the line with while, also in the if. I have read about borrowing, which I understand decently, but this thing I don't understand.
I am not "copying" the value at all, so I don't see anywhere were I could lose ownership of the variables.
Mul (and the other arithmetic operators) take the parameters by value, so i * i move the value i (this is not a problem for primitive numbers because they implement Copy - BigInt does not).
As Mul is implemented for (two) &BigInt, you can do the multiplication (and the other arithmetic operations) with &:
use num::*;
fn factors(number: &BigInt) -> Vec<BigInt> {
let mut n = number.clone();
let mut i = BigInt::from(2);
let mut factors = Vec::new();
while &i * &i <= n {
if (&n % &i) == BigInt::one() {
i = i + BigInt::one();
} else {
n = n / &i;
factors.push(i.clone());
}
i = i + BigInt::one();
}
if n > i {
factors.push(n);
}
factors
}
Note that I also made some simplifications, like omitting the type on Vec::new and using BigInt::from (cannot fail).
Remember that operators in Rust are just syntactic sugar for function calls.
a + b translates to a.add(b).
Primitive types such as i32 implement the trait Copy. Thus, they can be copied into such an add function and do not need to be moved.
I assume the BigInt type you are working with does not implement this trait.
Therefore, in every binary operation you are moving the values.
I'm just learning rust, henceforth this question has probably some trivial answer.
I want to access to individual digits of a rust BigUint. This is for a project Euler puzzle asking the sum of these digits.
I did it like below:
let mut f = func(100);
let mut total: BigUint = Zero::zero();
while f > FromPrimitive::from_uint(0).unwrap() {
let digit = f % FromPrimitive::from_uint(10).unwrap();
f = f / FromPrimitive::from_uint(10).unwrap();
total = total + digit;
}
println!("");
println!("Sum of digits of func(100) = {}", total);
It works, but it's quite frustrating, because I believe these digits are internaly stored as an array, but I can't access to them directly because the underlying data member is private.
Is there any way to do that in a more straightforward way ?
The internal storage of BigUint is a vector of BigDigit, which is an alias for u32, so it probably won't help you a lot to get the sum of base 10 digits.
I doubt casting to String would be an efficient way to compute this, but you can make it a little more straightforward using the div_rem() method from trait Integer. (After all, casting to String does this computation internally.)
extern crate num;
use std::num::Zero;
use num::bigint::BigUint;
use num::integer::Integer;
fn main() {
let mut f = func(100);
let mut total: BigUint = Zero::zero();
let ten: BigUint = FromPrimitive::from_uint(10).unwrap();
while f > Zero::zero() {
let (quotient, remainder) = f.div_rem(&ten);
f = quotient;
total = total + remainder;
}
println!("");
println!("Sum of digits of func(100) = {}", total);
}
I ended doing it using to_string() as suggested by #C.Quilley. But as #Levans pointed out the internal implementation merely perform a div_rem. I still prefer that version because I see it as more readable and straightforwad.
extern crate num;
use num::bigint::BigUint;
// What this function does is irrelevant
fn func(n: uint) -> BigUint {
let p : BigUint = FromPrimitive::from_uint(n).unwrap();
p*p*p*p*p*p*p*p*p*p*p*p*p
}
fn main() {
let n = 2014u;
let f = func(n);
let total: uint = f.to_string().as_slice().chars()
.fold(0, |a, b| a + b as uint - '0' as uint);
println!("Sum of digits of func({}) = {} : {}", n, f, total);
}