446,327 Members | 1,782 Online
Need help? Post your question and get tips & solutions from a community of 446,327 IT Pros & Developers. It's quick & easy.

# How to set a perticular bit in a given byte?

 P: 3 I have a character byte.I have to set any bit from it to 1.I am passing a bit position through a function say set_bits(bit_position) .Now give me a c code for this.Also i want to reset this bit again to 0, how can I do it.? Nov 3 '06 #1
5 Replies

 Expert 100+ P: 1,510 you use the bit operators | (OR) to set a bit and & (AND) to reset a bit, e.g. the program Expand|Select|Wrap|Line Numbers // set and reset bits #include  int main() {     int y=0;     printf("y=%x \n", y);     y = y | 1;        // set bit 0     printf("y=%x \n", y);     y = y | 8;       // set bit 3     printf("y=%x \n", y);       int x=0xff;     printf("x=%x \n", x);     x = x & ~1;        // clear bit 0     printf("x=%x \n", x);     x = x & ~8;       // clear bit 3     printf("x=%x \n", x);  return 0; }    when run gives the output y=0 y=1 y=9 x=ff x=fe x=f6 Nov 3 '06 #2

 P: 3 Thanks....but could you please explain it in detail..? Nov 3 '06 #3

 Expert 100+ P: 1,510 The following truth table shows the logical operations AND, OR (inclusive or) and EOR (exclusive or) on two bits A and B: Expand|Select|Wrap|Line Numbers              +----------------------------------+            ¦  A  ¦  B  ¦  AND  ¦  OR  ¦  EOR  ¦            +-----+-----+-------+------+-------¦            ¦  0  ¦  0  ¦   0   ¦  0   ¦   0   ¦            ¦  0  ¦  1  ¦   0   ¦  1   ¦   1   ¦            ¦  1  ¦  0  ¦   0   ¦  1   ¦   1   ¦            ¦  1  ¦  1  ¦   1   ¦  1   ¦   0   ¦            +----------------------------------+   & bitwise and operator& bitwise and operatorThe C bitwise logical operators & (and), | (or) and ^ (exclusive or) have two integral operands and perform the specified operation upon the corresponding bits in each operand. Expand|Select|Wrap|Line Numbers     k = i & j;      /* logical AND, i & j, assign result to k */      k = i | j;       /* logical inclusive OR, i | j, assign result to k */     k = i ^ j;       /* logical exclusive OR, i ^ j, assign result to k */   For example, assuming that i and j are byte sized variables having the values 00001010 and 01001100 respectively, the following table shows the result of &, | and ^ operations. Expand|Select|Wrap|Line Numbers +------------------------------------------------------------+ ¦  operation   ¦  operator  ¦  example operation  ¦  result  ¦ +--------------+------------+---------------------+----------¦ ¦     AND      ¦   i & j    ¦ 00001010 & 01001100 ¦ 00001000 ¦  ¦              ¦            ¦                     ¦          ¦  ¦ inclusive OR ¦   i | j    ¦ 00001010 | 01001100 ¦ 01001110 ¦  ¦              ¦            ¦                     ¦          ¦  ¦ exclusive OR ¦   i ^ j    ¦ 00001010 ^ 01001100 ¦ 01000110 ¦ +------------------------------------------------------------+       e.g. a program to set bits Expand|Select|Wrap|Line Numbers #include  int main() {     int bit, z=0;;      while(1)       {       printf("enter bit to set ? ");       scanf("%d", &bit);       z = z | (1 << bit);       printf("z=%x \n", z);       }   return 0; }   the loop 1. read the bit number, e.g. 5 2. shift 1 left by that number of bits, e.g. 0x20 3. OR it into z a run gave: enter bit to set ? 5 z=20 enter bit to set ? 0 z=21 Another bitwise operator is unary ~ (not or one's complement) which inverts the value of every bit in the operand, i.e. 0 becomes 1 and 1 becomes 0, e.g. for 00001010 the one's complement would be 11110101 i = ~i Nov 3 '06 #4

 P: 3 Thank you so much......keep in touch.......bye for now Nov 4 '06 #5

 P: 1 hi i have a interview on 2/may. i want the code for setting any bit i tried the above probs but its not running . i request u to plz send the correct n running code with explanation plz send it as soon as possible. on preety.jks25@gmail.com bye Jun 1 '07 #6