By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,327 Members | 1,782 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
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
  1. // set and reset bits
  2. #include <stdio.h>
  3. int main()
  4. {
  5.     int y=0;
  6.     printf("y=%x \n", y);
  7.     y = y | 1;        // set bit 0
  8.     printf("y=%x \n", y);
  9.     y = y | 8;       // set bit 3
  10.     printf("y=%x \n", y);
  11.  
  12.     int x=0xff;
  13.     printf("x=%x \n", x);
  14.     x = x & ~1;        // clear bit 0
  15.     printf("x=%x \n", x);
  16.     x = x & ~8;       // clear bit 3
  17.     printf("x=%x \n", x);
  18.  return 0;
  19.  
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
  1.  
  2.            +----------------------------------+
  3.              A    B    AND    OR    EOR  
  4.            +-----+-----+-------+------+-------
  5.              0    0     0     0      0   
  6.              0    1     0     1      1   
  7.              1    0     0     1      1   
  8.              1    1     1     1      0   
  9.            +----------------------------------+
  10.  
& 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
  1.     k = i & j;      /* logical AND, i & j, assign result to k */ 
  2.     k = i | j;       /* logical inclusive OR, i | j, assign result to k */
  3.     k = i ^ j;       /* logical exclusive OR, i ^ j, assign result to k */
  4.  
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
  1. +------------------------------------------------------------+
  2.   operation     operator    example operation    result  
  3. +--------------+------------+---------------------+----------
  4.      AND         i & j     00001010 & 01001100  00001000  
  5.                                                           
  6.  inclusive OR    i | j     00001010 | 01001100  01001110  
  7.                                                           
  8.  exclusive OR    i ^ j     00001010 ^ 01001100  01000110 
  9. +------------------------------------------------------------+    
  10.  
e.g. a program to set bits
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. int main()
  3. {
  4.     int bit, z=0;;
  5.      while(1)
  6.       {
  7.       printf("enter bit to set ? ");
  8.       scanf("%d", &bit);
  9.       z = z | (1 << bit);
  10.       printf("z=%x \n", z);
  11.       } 
  12.  return 0;
  13. }
  14.  
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

Post your reply

Sign in to post your reply or Sign up for a free account.