0

There's a variable:

char segment = 0;

After 1 or with bit 15, segment = 1;

Just means this bit check already.

Question is how to cancel the mark of bit 15 (set back to 0)?

Use "~"?

Durandal
  • 5,575
  • 5
  • 35
  • 49
Yun
  • 15
  • 1
  • 7
  • 4
    `char` only has 8 bits (not 15) – mvp Mar 20 '13 at 06:10
  • 2
    @mvp is correct - also, see here http://stackoverflow.com/q/47981/2065121 – Roger Rowland Mar 20 '13 at 06:11
  • sorry, I just started learning C language. 0 0 0 1 set back to 0 0 0 0 – Yun Mar 20 '13 at 06:14
  • `char` does **NOT** only have 8 bits, this is a property of the implementation which you can find out by using `CHAR_BIT` from `limits.h`. Please don't perpetuate inaccuracies. – paxdiablo Mar 20 '13 at 06:19
  • correction, @paxdiablo is correct, one old dog, one new trick http://stackoverflow.com/a/3200969/2065121 – Roger Rowland Mar 20 '13 at 07:20
  • Eitherway, from the subsequent comment it seems that @Yun actually means the *least significant bit*, normally designated bit zero. Duplicate of http://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit-in-c-c – Clifford Feb 27 '14 at 22:55

2 Answers2

1

Following program sets bit, clears bit and toggles bit

#include<stdio.h>

void main(void)
{
unsigned int byte;
unsigned int bit_position;
unsigned int tempbyte = 0x01;
//get the values of the byte and the bit positions 
//set bit
byte = (byte | (tempbyte << bit_position));// set the bit at the position given by bit_position
//clear bit
byte = (byte & ~(tempbyte << bit_position));//clear the bit at the position given by bit_position
//toggle bits
byte = (byte ^ (tempbyte << bit_position));//toggle the bit at the position given by bit_position
}
Nanobrains
  • 275
  • 1
  • 3
  • 11
0

To get rid of the MSB of an 8-bit character for example, you can AND with 0x7F

e.g. segment = segment & 0x7F;

To dynamically produce the mask, you can use bit shifting operations (i.e. the << operator).

Shiv
  • 1,274
  • 1
  • 19
  • 23