-3

I have a binary value equivalent 1 110 11 i need to set the highlighted bits to 0 101 00 , i need a resultant value as 1 101 11. How to set these bits and rest still remaining the same.

Manu
  • 5,534
  • 6
  • 32
  • 42
  • 1
    have you learned about binary arithmetic? How to use `&` and `|` with masks to set/clear bits? Start there then come back with a question on where you ran into trouble. – Doug T. Feb 20 '13 at 17:55
  • [what have you tried? :)](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – Adel Boutros Feb 20 '13 at 17:56
  • 1
    It's called [masking bits](http://en.wikipedia.org/wiki/Mask_(computing)) – Mike Feb 20 '13 at 17:57
  • this question deserves more attention, it isn't the easiest of bit operations – 75inchpianist Feb 20 '13 at 18:12
  • 1
    @75inchpianist - I would disagree. If you have a question about setting bits, it's probably because of a school assignment.. and bit masking is a very basic operation so it should have been taught. Regardless of that, the question shows no effort as it's been asked/answered on SO several times, even a google search for the title of this question would have provided results. No effort + better versions of the same already on SO = doesn't deserve attention or upvotes. – Mike Feb 20 '13 at 18:37

2 Answers2

4

This should help http://en.wikipedia.org/wiki/Bitwise_operations_in_C

If you need working example let me know but i encourage you to figure it out on your own.

Martin
  • 1,752
  • 2
  • 13
  • 21
0
unsigned char my_byte = 0x3B; // 0b00111011

// clear the bits
my_byte &= 0xE3;

// set the bits
my_byte |= 0x14;

You'll find many people have many different preferences on how to write the 0xE3 and 0x14. Some like to shift bits, however ultimately this is the code that should be produced.

Josh Petitt
  • 9,371
  • 12
  • 56
  • 104