Bitwise Operators: Aplication

N

noridotjabi

I'm learning to program in C and any tutorial or book that I read likes
to briefly touch on birdies operators and then move on without giving
any sort of example application of them. Call me what you will but I
cannot seem to see the purpose for bitwise operators. Especially the
operators bitwise OR ( | ) and bitwise AND ( & ), I'm just not getting
it. I have searched around and really haven't found anything that gave
explanation to why these were necessary to learn or even adequately
describe what they do. Therefor any explanation or recommendation of
the bitwise operators would be greatly appreciated by me. Thanks in
advanced.
Nori

**If I'm off topic tell me**
 
A

AG

In typical C coding, I never use either of these commands. But it is
common in microprocessor applications that you would look at just one
or two bits from a register. To look at just one or two bits at a time
is called masking. Suppose you have a byte-sized register (reg) but
you only need to look at two bits (bit3 and bit7). The following
bitwise AND statement

k = reg & %10001000

will exclude all bits except bit3 and bit7, no matter what value is in
reg.

k=%X000X000

I hope this helps.
 
B

Ben C

I'm learning to program in C and any tutorial or book that I read likes
to briefly touch on birdies operators and then move on without giving
any sort of example application of them. Call me what you will but I
cannot seem to see the purpose for bitwise operators. Especially the
operators bitwise OR ( | ) and bitwise AND ( & ), I'm just not getting
it. I have searched around and really haven't found anything that gave
explanation to why these were necessary to learn or even adequately
describe what they do.

What they do is operate on their operands one bit at a time:

uint32_t a = 42;
uint32_t b = 87;

In binary, these two numbers look like this:

00000000000000000000000000101010
00000000000000000000000001010111

Now, we can "AND" bits one at a time:

00000000000000000000000000101010 &
00000000000000000000000001010111
--------------------------------
00000000000000000000000000000010
(we get a 1 in each column where both inputs are 1)

which is 2 in decimal, or "OR" bits one at a time:

00000000000000000000000000101010 |
00000000000000000000000001010111
--------------------------------
00000000000000000000000001111111
(we get a 1 in each column where either input is 1)

Which is 127 in decimal.

What do you use them for? Among other things, quite often for
collections of boolean values, one or more of which may be true e.g.:

#define BIG 1U
#define FURRY (1U << 1)
#define STRIPED (1U << 2)
#define SPOTTED (1U << 3)
#define EVERYTHING (BIG | FURRY | STRIPED | SPOTTED)

uint32_t leopard = BIG | FURRY | SPOTTED;
uint32_t tiger = BIG | FURRY | STRIPED;
uint32_t mouse = FURRY;
uint32_t zebra = BIG | STRIPED;

Then you can do various things, like test for presence of a property:

if (animal & FURRY)

or for presence of either one out of two properties:

if (animal & (FURRY | SPOTTED))

etc.

You can set a property like this:

animal |= FURRY;

or unset one like this:

animal &= ~FURRY;

and other things.

Exercise: how to toggle a flag?
 
F

Fred Kleinschmidt

I'm learning to program in C and any tutorial or book that I read likes
to briefly touch on birdies operators and then move on without giving
any sort of example application of them. Call me what you will but I
cannot seem to see the purpose for bitwise operators. Especially the
operators bitwise OR ( | ) and bitwise AND ( & ), I'm just not getting
it. I have searched around and really haven't found anything that gave
explanation to why these were necessary to learn or even adequately
describe what they do. Therefor any explanation or recommendation of
the bitwise operators would be greatly appreciated by me. Thanks in
advanced.
Nori

**If I'm off topic tell me**
Bitwise AND and OR are very useful for defining a flag variable.
Let's say I have a generic menu-builder function for, say, text editing:
Widget EditMenu( Widget parent, int flag );

Now in that function I can do something like:
if ( flag & CUT ) {
/* include CUT menu item */
}

if ( flag & COPY ) {
/* include COPY item */
}
.... etc.
where I have defined:
#define CUT 1
#define COPY 2
#define PASTE 4
#define CAPITALIZE 8
... etc (or could use shift operators to define these flag items.

Then I call my menu function:
Widget menu = EditMenu( parent, CUT | COPY | PASTE );
the above would create my menu with those three items.
I could also call it as:
Widget menu = EditMenu( parent, CUT | CAPITALIZE );
to only include CUT and CAPITALIZE functionality.

This is only one of a multitude of uses for these operators.
 
R

Rod Pemberton

I'm learning to program in C and any tutorial or book that I read likes
to briefly touch on birdies operators and then move on without giving
any sort of example application of them. Call me what you will but I
cannot seem to see the purpose for bitwise operators. Especially the
operators bitwise OR ( | ) and bitwise AND ( & ), I'm just not getting
it. I have searched around and really haven't found anything that gave
explanation to why these were necessary to learn or even adequately
describe what they do. Therefor any explanation or recommendation of
the bitwise operators would be greatly appreciated by me. Thanks in
advanced.

The bitwise operators allow you to set, clear, invert, and shift bits in a
value. You may need to do this for many situations: such as programming
hardware registers, graphics, encryption, or extracting an error code.

bitwise or, | , allows you to set bits, any bits in the "oring" value which
are one get set
bitwise and, &, allows you to clear bits, any bits in the "anding" value
which are zero get cleared
bitwise xor, ^, allows you to invert bits, any bits in the "xoring" value
which are set get inverted
bitwise negation,~, allows you to invert all bits, all bits in the "noting"
value are inverted
left bitshift, <<, allows you to shift bits left
right bitshift, >>, allows you to shift bits right

Let's say you want to convert an ASCII unsigned char from lower case to
upper case. The lower case values are +32 above the uppercase values. 32
is nicely expressed as 0x20 hex or 00100000 binary. To convert 'a' to 'A",
we'd use the bitwise and to clear that single bit (bit 5):

val='a';
val&=~0x20; /* 'a' becomes 'A' in ASCII */
/* ~0x20 is 11011111, bit is zero to clear */
/* 'a' is 0x61 hex or 01100001 binary */
/* 'A' is 0x41 hex or 01000001 binary */

To convert 'A' back to 'a', we'd use the bitwise or to set that single bit (
bit 5):

/* val='A'; from above */
val|=0x20; /* 'A' becomes 'a' in ASCII */
/* 0x20 is 00100000, bit is one to set */
/* 'A' is 0x41 hex or 01000001 binary */
/* 'a' is 0x61 hex or 01100001 binary */


HTH,

Rod Pemberton
 
F

Flash Gordon

AG wrote:

Please provide context. See http://clc-wiki.net/wiki/Intro_to_clc for
information about this group and how to use Google to do the right thing.
In typical C coding, I never use either of these commands. But it is
common in microprocessor applications that you would look at just one
or two bits from a register. To look at just one or two bits at a time
is called masking.

True. It is also used when dealing with various communications protocols
and in lots of other places.

<OT>
Masking to select mode bits from one of the fields stat produces on *nix
is one such other instance.
> Suppose you have a byte-sized register (reg) but
you only need to look at two bits (bit3 and bit7). The following
bitwise AND statement

k = reg & %10001000

will exclude all bits except bit3 and bit7, no matter what value is in
reg.

k=%X000X000

I hope this helps.

It won't. I don't know (or want to know) what language it is but it
isn't C. I'm guessing that whatever language it is uses % to introduce
binary literals, but standard C (which is what we deal with here) has no
support for binary literals. Bit masks are generally specified in hex,
although there is nothing to stop you using octal or even decimal should
you chose.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc

Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,829
Latest member
KimberAlli

Latest Threads

Top