// bit_operator_test.c - short demonstration of the
// possibilities of bit-operators and their usage

unsigned char one_byte_ = 0;
unsigned char another_byte_ = 0;
unsigned char bitmask_ = 0;
unsigned int large_value_ = 0;

main()
{
  // set one_byte_ to the binary value 11001010
  one_byte_ = 0xca;
  // we want to set the 3rd bit from the right
  bitmask_ = 0x04; // 00000100
  one_byte_ = one_byte_ | bitmask_;
  // the result should be 11001110 (=0xce) - check it:
  printf("after or-mask one_byte_ contains 0x%x\n",one_byte_);
  // clear the 3rd bit from the right again
  bitmask_ = ~bitmask_; // inverse bitmask: 11111011
  one_byte_ = one_byte_ & bitmask_;
  // the result should be 11001010 again (=0xca) - check it:
  printf("after and-mask one_byte_ contains 0x%x\n",one_byte_);
  // different possibility to check, if a bit is set:
  printf("is the 3rd bit from the right set? -> %x\n",
          one_byte_ & 0x04);

  // try a little bit-shifting and masking...
  one_byte_ = 0x59; // 01011001
  another_byte_ = 0x8f; // 10001111
  // set the initial value
  large_value_ = one_byte_;
  printf("initial assignment results in: 0x%x\n",large_value_);
  // shift it to the left
  large_value_ = large_value_ << 8;
  printf("after 8 bit left-shift: 0x%x\n",large_value_);
  // mask-in the second value on the right side
  large_value_ = large_value_ | another_byte_;
  printf("after masking in the rest: 0x%x\n",large_value_);
}
