Hello, I have some problems with this code. It says: "Main.scala:1: error: expected class or object definition def extend(p: Long): Long = { ^ Main.scala:21: error: expected class or object definition def ok(x: Long): Boolean = { ^ 2 errors Could you please help me fix it? Code: def extend(p: Long): Long = { // Count the number of 1's in the payload var count = 0 for (i <- 0 until 63) { if ((p & (1L << i)) != 0) { count += 1 } } // Set the parity bit to 0 or 1 depending on whether the number of 1's is even or odd if (count % 2 == 0) { p | (1L << 63) } else { p & ~(1L << 63) } } def ok(x: Long): Boolean = { // Count the number of 1's in the extended payload var count = 0 for (i <- 0 until 64) { if ((x & (1L << i)) != 0) { count += 1 } } // Return true if the number of 1's is even and false if it is odd count % 2 == 0 }
Hello, I have some problems with this code.
It says:
"Main.scala:1: error: expected class or object definition
def extend(p: Long): Long = {
^
Main.scala:21: error: expected class or object definition
def ok(x: Long): Boolean = {
^
2 errors
Could you please help me fix it?
Code:
def extend(p: Long): Long = {
// Count the number of 1's in the payload
var count = 0
for (i <- 0 until 63) {
if ((p & (1L << i)) != 0) {
count += 1
}
}
// Set the parity bit to 0 or 1 depending on whether the number of 1's is even or odd
if (count % 2 == 0) {
p | (1L << 63)
} else {
p & ~(1L << 63)
}
}
def ok(x: Long): Boolean = {
// Count the number of 1's in the extended payload
var count = 0
for (i <- 0 until 64) {
if ((x & (1L << i)) != 0) {
count += 1
}
}
// Return true if the number of 1's is even and false if it is odd
count % 2 == 0
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images