public class Inverter { public static byte InvertBits(byte value) { uint uintValue; uintValue = value; return (byte) ~uintValue; } }
unsigned char invertbits(unsigned char value)- public class Inverter
- {
return value ^ 0xFF;}- public static byte InvertBits(byte value)
- {
- uint uintValue;
- uintValue = value;
- return (byte) ~uintValue;
- }
- }
using NUnit.Framework; [TestFixture] public class InvertBitsTest { [Test, Description("should invert all bits")] public void ShouldInvert() { Assert.AreEqual(252, Inverter.InvertBits(3)); Assert.AreEqual(255, Inverter.InvertBits(0)); Assert.AreEqual(0, Inverter.InvertBits(255)); Assert.AreEqual(248, Inverter.InvertBits(7)); Assert.AreEqual(127, Inverter.InvertBits(128)); for (byte b = 0; b < 255; b++) Assert.AreEqual((byte)~b, Inverter.InvertBits(b)); } }
#include <criterion/criterion.h>- using NUnit.Framework;
// replace with the actual method being testedunsigned char invertbits(unsigned char value);Test(the_multiply_function, should_pass_all_the_tests_provided) {cr_assert_eq(invertbits(3), 252);cr_assert_eq(invertbits(255), 0);cr_assert_eq(invertbits(7), 248);cr_assert_eq(invertbits(128), 127);- [TestFixture]
- public class InvertBitsTest
- {
- [Test, Description("should invert all bits")]
- public void ShouldInvert()
- {
- Assert.AreEqual(252, Inverter.InvertBits(3));
- Assert.AreEqual(255, Inverter.InvertBits(0));
- Assert.AreEqual(0, Inverter.InvertBits(255));
- Assert.AreEqual(248, Inverter.InvertBits(7));
- Assert.AreEqual(127, Inverter.InvertBits(128));
- for (byte b = 0; b < 255; b++)
- Assert.AreEqual((byte)~b, Inverter.InvertBits(b));
- }
- }
Invert all bits of a value and return the inverted value.
Examples:
invertbits(3) => returns 252 (11111100)
invertbits(255) => returns 0 (00000000)
unsigned char invertbits(unsigned char value)
{
return value ^ 0xFF;
}
#include <criterion/criterion.h>
// replace with the actual method being tested
unsigned char invertbits(unsigned char value);
Test(the_multiply_function, should_pass_all_the_tests_provided) {
cr_assert_eq(invertbits(3), 252);
cr_assert_eq(invertbits(255), 0);
cr_assert_eq(invertbits(7), 248);
cr_assert_eq(invertbits(128), 127);
}
Set a bit in value starting with bit 0 as the lowest bit.
Every bit value above 7 will be ignored and the function returns
the parameter value without a modification.
Examples:
setbit (3, 2) => returns: 7 (00000111)
setbit (3, 3) => returns: 11 (00001011)
setbit (3, 8) => returns: 3 (00000011)
unsigned char setbit (unsigned char value, unsigned int bit)
{
return value | (1 << bit);
}
#include <criterion/criterion.h>
// replace with the actual method being tested
unsigned char setbit(unsigned char,unsigned int);
Test(the_multiply_function, should_pass_all_the_tests_provided) {
cr_assert_eq(setbit(1, 1), 3);
cr_assert_eq(setbit(1, 0), 1);
cr_assert_eq(setbit(0, 1), 2);
cr_assert_eq(setbit(3, 2), 7);
cr_assert_eq(setbit(3, 3), 11);
cr_assert_eq(setbit(0, 7), 128);
cr_assert_eq(setbit(0, 8), 0);
}