473,388 Members | 1,390 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,388 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 76349
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.