Skip to content

Instantly share code, notes, and snippets.

@Zerophase
Last active August 14, 2018 00:25
Show Gist options
  • Save Zerophase/89038f8a96c39730447fa6d2529bffd4 to your computer and use it in GitHub Desktop.
Save Zerophase/89038f8a96c39730447fa6d2529bffd4 to your computer and use it in GitHub Desktop.
int any_bit_eq_1(int x)
{
return !!(x & 0xFFFFFFFF);
}
int any_bit_eq_0(int x)
{
return !!(~x & 0xFFFFFFFF);
}
int any_bit_in_lsb_eq_1(int x)
{
return !!(x & 0xFF);
}
int any_bit_in_msb_eq_0(int x)
{
int shift_msb = (sizeof(int) - 1) << 3;
int msb = x >> shift_msb;
return !!(~msb & 0xFF);
}
void hw_2_61()
{
// int is 32 bits
// write c expressions that evaluate to 1 when the following conditions are true and to 0 when
// they are false. Assume x is of type int.
// A. Any bit of x equals 1.
int xa = 0x12345678;
int x_eq_1 = any_bit_eq_1(xa);
printf("any_bit_of_x_eq_1 true: %d\n", x_eq_1);
xa = 0x1;
x_eq_1 = any_bit_eq_1(xa);
printf("any_bit_of_x_eq_1 true (mix 0 and 1): %d\n", x_eq_1);
xa = 0x0;
x_eq_1 = any_bit_eq_1(xa);
printf("any_bit_of_x_eq_1 false: %d\n", x_eq_1);
// B. any bit of x equals 0.
int xb = 0x0;
int x_eq_0 = any_bit_eq_0(xb);
printf("any_bit_of_x_eq_0 true: %d\n", x_eq_0);
xb = 0x1;
x_eq_0 = any_bit_eq_0(xb);
printf("any_bit_of_x_eq_0 true (mix 0 and 1): %d\n", x_eq_0);
xb = 0xFFFFFFFF;
x_eq_0 = any_bit_eq_0(xb);
printf("any_bit_of_x_eq_0 false: %d\n", x_eq_0);
// C. Any bit in the least significant byte of x equals 1.
int xc = 0x00000001;
int lsb_any_bit_1 = any_bit_in_lsb_eq_1(xc);
printf("lsb has a 1 (true): %d\n", lsb_any_bit_1);
xc = 0x00000000;
lsb_any_bit_1 = any_bit_in_lsb_eq_1(xc);
printf("lsb is 0 (false): %d\n", lsb_any_bit_1);
xc = 0x123456FF;
lsb_any_bit_1 = any_bit_in_lsb_eq_1(xc);
printf("lsb is all 1 (true): %d\n", lsb_any_bit_1);
xc = 0xffffff00;
lsb_any_bit_1 = any_bit_in_lsb_eq_1(xc);
printf("lsb has no 1 (false): %d\n", lsb_any_bit_1);
// D. Any bit in the most significant byte of x equals 0.
int xd = 0x00234567;
int msb_any_bit_0 = any_bit_in_msb_eq_0(xd);
printf("msb is 0: %d\n", msb_any_bit_0);
xd = 0xFE234567;
msb_any_bit_0 = any_bit_in_msb_eq_0(xd);
printf("msb is 0 (mix 1 and 0 in msb): %d\n", msb_any_bit_0);
xd = 0xFF234567;
msb_any_bit_0 = any_bit_in_msb_eq_0(xd);
printf("msb is 0 (all 1 in msb) false: %d\n", msb_any_bit_0);
// may not use == or != tests
return 0;
}
int any_bit_eq_1(int x)
{
return !~x;
}
int any_bit_eq_0(int x)
{
return !x;
}
int any_bit_in_lsb_eq_1(int x)
{
return !~(x | ~0xFF);
}
int any_bit_in_msb_eq_0(int x)
{
int shift_msb = (sizeof(int) - 1) << 3;
int msb = x >> shift_msb;
return !(msb & 0xFF);
}
int main()
{
// int is 32 bits
// write c expressions that evaluate to 1 when the following conditions are true and to 0 when
// they are false. Assume x is of type int.
// A. Any bit of x equals 1.
int xa = 0x12345678;
int x_eq_1 = any_bit_eq_1(xa);
printf("any_bit_of_x_eq_1 true: %d\n", x_eq_1);
xa = 0x1;
x_eq_1 = any_bit_eq_1(xa);
printf("any_bit_of_x_eq_1 true (mix 0 and 1): %d\n", x_eq_1);
xa = 0x0;
x_eq_1 = any_bit_eq_1(xa);
printf("any_bit_of_x_eq_1 false: %d\n", x_eq_1);
// B. any bit of x equals 0.
int xb = 0x0;
int x_eq_0 = any_bit_eq_0(xb);
printf("any_bit_of_x_eq_0 true: %d\n", x_eq_0);
xb = 0x1;
x_eq_0 = any_bit_eq_0(xb);
printf("any_bit_of_x_eq_0 true (mix 0 and 1): %d\n", x_eq_0);
xb = 0xFFFFFFFF;
x_eq_0 = any_bit_eq_0(xb);
printf("any_bit_of_x_eq_0 false: %d\n", x_eq_0);
// C. Any bit in the least significant byte of x equals 1.
int xc = 0x00000001;
int lsb_any_bit_1 = any_bit_in_lsb_eq_1(xc);
printf("lsb has a 1 (true): %d\n", lsb_any_bit_1);
xc = 0x00000000;
lsb_any_bit_1 = any_bit_in_lsb_eq_1(xc);
printf("lsb is 0 (false): %d\n", lsb_any_bit_1);
xc = 0x123456FF;
lsb_any_bit_1 = any_bit_in_lsb_eq_1(xc);
printf("lsb is all 1 (true): %d\n", lsb_any_bit_1);
xc = 0xffffff00;
lsb_any_bit_1 = any_bit_in_lsb_eq_1(xc);
printf("lsb has no 1 (false): %d\n", lsb_any_bit_1);
// D. Any bit in the most significant byte of x equals 0.
int xd = 0x00234567;
int msb_any_bit_0 = any_bit_in_msb_eq_0(xd);
printf("msb is 0: %d\n", msb_any_bit_0);
xd = 0xFE234567;
msb_any_bit_0 = any_bit_in_msb_eq_0(xd);
printf("msb is 0 (mix 1 and 0 in msb): %d\n", msb_any_bit_0);
xd = 0xFF234567;
msb_any_bit_0 = any_bit_in_msb_eq_0(xd);
printf("msb is 0 (all 1 in msb) false: %d\n", msb_any_bit_0);
// may not use == or != tests
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment