NEWTON

NEWTON

# How to make Bitwise operations in Cairo Lang using %builtins bitwise, bitwise_xor?

3 months ago

114

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

#17daysOfCairocairo-beginnerscairoplayground

Newton

3 months ago

0

# 1. Requirement

Calculate `xor` of the last two hexadecimal digits.

For example: `0x1234` -> `3 xor 4`.

# 2. Solution

• 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`.

# 3. Source code

``````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);
}
``````

# Note

``````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);
^*********************************^
``````

``````return bitwise_xor(digit0, digit1);
``````

I use () to get felt datatype and return result.

``````let (res) = bitwise_xor(digit0, digit1);
return (res = res);
``````

3 months ago