Why is 2 raised to 4 equal 10? [closed] - rust

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
I was looking into bitwise operators in rust, and I found that
println!("{:X}", 1 << 4);
prints out 10, but 2^4 should equal 16.
Further experimentation, using powers:
let base: i32 = 2;
for i in 1..=5 {
print!("{:X} ", base.pow(i));
}
will print out
2 4 8 10 20 when it should print out 2 4 8 16 32
Just wondering if you can point me out to anything in the Rust docs that highlights why binary in Rust works like this? And what can I use to do 2^4 = 16?

{:X} print numbers in hexadecimal.
So it prints 10 in base 16 which is 16, the expected answer.
To get the expected result change {:X} to {}.

Related

Getting the sum of all uneven indices of an int [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 days ago.
Improve this question
I'm new to rust and tried writing a function that adds all digits at the uneven positions together (For context: I'm trying to solve this Daily programmer #370. But somehow my results are off by a bit and I can't wrap my head around what#s going on.
number.to_string().chars()
.enumerate()
.filter(|(i, _)| i % 2 != 0)
.map(|(_, c)| c as u64 - 48)
.sum::<u64>()
Now if I input 4210000526 I get 13 as a result, although I should get 15. For some reason this only happens with a filter for uneven positions, "i % 2 == 0" works perfectly fine.

Find All possible solutions for an expression [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 days ago.
Improve this question
I'm currently looking for how to get all possible solutions for an expression in Rust
// example expression
x * y = 8
// more complex expression
4x^2 + y^2 = 961
and now the results should be for the first expression:
[(1,8), (2,4), (4,2), (8,1)]
Is something like this possible without going through every number in a for loop?
// this would be a way (but i guess its a bad way)
for i in 1..8 {
if 8 % i == 0 {
// add this i option to the vector and get the division factor
let division_factor 8 / i
possible_solutions.push((i, division_factor));
}
}
And this is surely not the best practice, and I'm looking for a way with a good performance.

Rust bitwise into u8 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I tried this but it doesn't work.
let result: u8 = opcode & 0xff;
the thing is opcode & 0xff will always return something in 0 -> 255 which would always fit in the u8 but the compiler raise the error expected u8 but found u16. Why did Rust raise the error?
Rust doesn't cast between types implicitly, you have to be explicit about the cast using the as keyword:
let result = opcode as u8;
Note that you can omit the AND operator, the as keyword will automatically truncate the number when converting from a larger type.

"logical operator" behaves unexpectedly in python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Why does the following behave unexpectedly in Python?
print('a' > 'b')
False
print('a' > 'A')
True
If you use >, <, >=, or <= on strings, they get compared by their ASCII value. The ASCII value of 'a' is 97, 'b' is 98, and 'A' is 65. So A is lower than a, because it's earlier in the ASCII table.

Issue when running a groovy script for "function_score"? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
def score = 0;
// terms: list of tokens
for(term in terms) {
q_term_freq = terms​.countBy { it }​[term]; // for frequency of each term in terms
term_freq = _index[field][term].tf();
doc_freq = _index[field][term].df();
score += term_freq * doc_freq * q_term_freq;
};
score;
When I run this I get an error `GroovyScriptExecutionException[MissingPropertyException[No such property: terms\u200b for class: Script86.
What is going wrong? AFAIK countBy is valid function.
\u200b is unicode for Zero Width Space.
Rewrite the script or make sure there is no unicode character present with terms.

Resources