Continue to Site

# What are bitwise operators in C used for ?

Status
Not open for further replies.

#### saudrehman

##### Member level 1
bitwise operators in C

can anybody explain the use of Bitwise operators like and,or,exor in C programming for microcontrollers.

Re: bitwise operators in C

you can read it in book programming for C.
Exp: "and"
1&1=1
1&0=0
0&1=0
0&0=0

bitwise operators in C

note that when you bitwise and (or, xor), each bit is anded (ored, xored) with the correspoding bit in the other word.

so 0b1010 & 0b1100 becomes 0b1000
or in hex 0xA & 0xC becomes 0x8

and is great for masking/clearing bits - say you want to zero all but the last three bits in a byte, call it val.
val = val & 0x7;

or is good for setting bits. for example, setting pins to input (1) or output (0):
TRISB = TRISB | 0b00011100; // sets pins 2, 3 & 4 to input, leave the others alone

I prefer to use binary repersentation when fiddling with bits in registers. makes it alot clear what's going on.

Phil

Re: bitwise operators in C

hi
this may help u

Bitwise Operators:

Operators that work on the individual bits within a variable are
called bitwise operators. Following is a table of all of these operators:

& bitwise AND
| bitwise Inclusive OR
^ bitwise Exclusive OR
~ one’s complement.
>> right shift
<< left shift

more details u can find in any c book

salam
Ahmed
www.i-g.org

Re: bitwise operators in C

i need a help on the same bitwise operations.
but this a little complicated. this is bitwise copying and pasting.

i need to copy a particular number of bits and paste at different loaction.
eg., A stream has 1000 bits. i am copying from 102 bit to 201 bit, then pasting(overwriting) at 305 th bit. is there an algorithm to do this.
regards,
shanmugavel

Re: bitwise operators in C

shanmugavel this is totally different subject.First we need to know how your 1000 bit stream is organized in the memory?

Re: bitwise operators in C

the 1000 bits(125 bytes) in memory are byte aligned stored.

hi if I wont to write 0b 00001010 to a memmery that holds 0bxxxx0000 and I dont wont to overwrite "x" data on the memmery and I don't know wot these x binary holds. how do I do? i C programming

Status
Not open for further replies.