`
//Return x with the n bits that begin at position p inverted (i.e. turn 0 into 1 and vice versa)
int invert (int x, int p, int n) {
}
int bitAnd (int x, int y) {
// implement x & y without using &
}
int bitXor (int x, int y) {
//implement x ^ y using only ~ and | ()
}
int sign(int x) {
//return 1 if positive, 0 if zero, and –1 if negative. ops allowed: ! ~ & ^ | + << >> ()
}
int conditional(int x, int y, int z) {
// if x is present return y if x is 0 return z. Or implment return x ? y : z. Ops allowed ! ~ & ^ | + << >> ()
}
int bang(int x) {
// bang(3)=0, bang(0)=1 ops allowed : ~ & ^ | + << >> ()
}
A king is planning a party in 7 days where he intends to use 1,000 barrels of wine, stored in a secret location. A spy from a neighboring country was caught adding poison to one of the barrels and was subsequently killed by the security guards. Here are the key points:
Help the security guards devise a strategy to identify and isolate the poisoned barrel.
There are 10 prisoners who are shown a box containing an infinite number of white and black hats. They are informed that the next morning, they will participate in an exercise:
As one of the prisoners, you need to devise a strategy to maximize the number of people who can be saved. One possible strategy is for each person in an even-numbered position to state the color of the hat of the person directly in front of them. Meanwhile, each person in an odd-numbered position will repeat what the previous person said. This approach can save at least five people, and with further improvements, it could potentially save even more.