NEWTON
Asked
8 months ago
128
views
0
Hello! This is day 10 of the 17 days of the Cairo Challenge. And I have no idea how to solve the playground exercise “Bitwise operations”. Perhaps you can help me?
// The bitwise builtin allows you to perform bitwise operations
// (and, or, xor) on integers up to 251 bits.
//
// The following code implements the function xor_last_hex_digits,
// which takes the two last hexadecimal digits and "xor"s them.
// For example, in 0x1234 the last two digits are 3 and 4,
// and their xor is 3^4=7.
//
// Fix the missing code and make sure it works.
%builtins bitwise
from starkware.cairo.common.bitwise import bitwise_and, bitwise_xor
from starkware.cairo.common.cairo_builtins import BitwiseBuiltin
// Returns the xor of the last two hexadecimal digits.
func xor_last_hex_digits{bitwise_ptr: BitwiseBuiltin*}(x: felt) -> (res: felt) {
// Fix and uncomment the line below.
// let (digit0) = ...
let (shifted_digit1) = bitwise_and(x, 0xF0);
tempvar digit1 = shifted_digit1 / 0x10;
return bitwise_xor(digit0, digit1);
}
func main{bitwise_ptr: BitwiseBuiltin*}() {
let (res) = xor_last_hex_digits(0x1234);
assert res = 7;
return ();
}
Answers to this question are a part of the ✨ 17 days of Cairo Lang with Playground & Newton. ✨
Vote for your favorite answer - the best answer will win a $10 award. A new day – a new reward! During the next 17 days, our goal is to attract more developers to the Cairo language and to systematize the knowledge of Cairo lang. Read rules
Newton
asked
8 months ago
0
Accepted answer
Calculate xor
of the last two hexadecimal digits.
For example: 0x1234
-> 3 xor 4
.
Calculate the last digit(digit0
): and
our number with 0xF
. To make it easy to understand, 0xF
is 0b1111
, if I and
0b1111
with a number, I will have its last 4 bits, equivalent to its last digit in hexadecimal format. We have to define the code to calculate digit0
.
Calculate the next-to-last digit(digit1
): and
our number with 0xF0
to remove all digits except digit1
. For example: 0x1234 and 0xF0 = 0x30
. Then divide result by 0x10
to remove last 0 digit. In this excercise, the code to calculate digit1
is already available.
Use the bitwise_xor
function to xor digit0
and digit1
.
func xor_last_hex_digits{bitwise_ptr: BitwiseBuiltin*}(x: felt) -> (res:felt) {
// Fix and uncomment the line below.
let (digit0) = bitwise_and(x, 0xF);
let (shifted_digit1) = bitwise_and(x, 0xF0);
tempvar digit1 = shifted_digit1 / 0x10;
let (res) = bitwise_xor(digit0, digit1);
return (res = res);
}
I receive error:
Error: code:24:5: Cannot convert the return type of bitwise_xor to the return type of xor_last_hex_digits.
return bitwise_xor(digit0, digit1);
^*********************************^
So instead of
return bitwise_xor(digit0, digit1);
I use () to get felt datatype and return result.
let (res) = bitwise_xor(digit0, digit1);
return (res = res);
answered
8 months ago
How to use get_fp_and_pc in Cairo Lang?
Cairo Lang / StarkNet: What are Revoked references? What is alloc_locals?
What is the difference between tempvar/let in Cairo Lang? How to use allow_locals and local?
How to write a function in Cairo Lang (StarkNet)?
How to make conditional expressions if..then..else in Cairo lang?
How to divide felt in Cairo Lang using unsigned_div_rem?
How to submit a StarkNet contract?
Block Chain
Does anybody have an idea what is `get_caller_address` returning on an L1 handler call?
Error compiling Pathfinder (rust)
Cairo Testing Error
Revoked References exercise from Cairo-lang
Has anyone implemented a minimal proxy in Cairo lang?
How do I create my own account abstraction using starknetjs?