473,320 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to set a perticular bit in a given byte?

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 76333
horace1
1,510 Expert 1GB
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
Thanks....but could you please explain it in detail..?
Nov 3 '06 #3
horace1
1,510 Expert 1GB
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
Thank you so much......keep in touch.......bye for now
Nov 4 '06 #5
check
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

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

Similar topics

6
by: Arnold Shore | last post by:
Folks, I get a TYPE MISMATCH complaint fm ASP when I do the following in trying to extract the high-order four bits. The complaint being that the strTemp value is a string. Well, yes - it's a...
20
by: adityavasishth | last post by:
hi all, Characters are basically implemented via integers,ex : '\0' is 0.But integers requires 2 bytes and the characters require only 1 byte.So,can anybody please tell me that how the...
16
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the...
25
by: Charles Law | last post by:
I thought this was going to be straight forward, given the wealth of conversion functions in .NET, but it is proving more convoluted than imagined. Given the following <code> Dim ba(1) As...
6
by: lovecreatesbeauty | last post by:
/* It seems that when an int with width of four bytes is assigned to a one byte width char, the first three bytes from left to right are discarded and the rightest byte is assigned to that char....
33
by: Benjamin M. Stocks | last post by:
Hello all, I've heard differing opinions on this and would like a definitive answer on this once and for all. If I have an array of 4 1-byte values where index 0 is the least signficant byte of a...
96
by: david ullua | last post by:
I am reading "Joel on Software" these days, and am in stuck with the question of "how to calculate bitsize of a byte" which is listed as one of the basic interview questions in Joel's book. Anyone...
45
by: Ajay | last post by:
Hi all,can you please tell the most efficient method to reverse a byte.Function should return a byte that is reversed.
9
by: RichG | last post by:
I'm working with a data stream of 8 bytes in an embedded application. In most cases the data is byte aligned so I can define a structure and then memcpy the data directly to the structure elements. ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.