473,224 Members | 1,999 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,224 software developers and data experts.

Bitwise Operators: Aplication

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**

Apr 24 '06 #1
5 5743
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.

Apr 24 '06 #2
On 2006-04-24, no*********@gmail.com <no*********@gmail.com> wrote:
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?
Apr 24 '06 #3

<no*********@gmail.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
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.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Apr 24 '06 #4
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.
</OT>
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
Apr 24 '06 #5

<no*********@gmail.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
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
Apr 24 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Michael B. Trausch | last post by:
I have a question regarding bitwise operators, I've been trying to figure this out for about two days now, and I just can't seem to get it. What I'm trying to do is use a variable to hold a bitmask...
11
by: Randell D. | last post by:
Why would one use bitwise operators? I can program in various languages in some shape or form (C++, PHP, some scripting) and I've heard/seen bitwise operators before, but never understood why...
4
by: Mike Hodkin | last post by:
As a beginning student of C++, books reference "bitwise operators" and give brief examples, but I have not read a good explanation of what they are used for. One reference mentioned that they are...
6
by: jas_lx | last post by:
The basic understanding of what bitwise operators (& ^ | >> << ) comes fairly simple, as long as one has a fundamental understanding of bits, bytes and binary. Having done some Win32...
2
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
37
by: James Radke | last post by:
Hello, I found some code that I could use in my application on the web, but it is written in C#, and I would like to convert it to VB. And I am having problems with one section. Is there...
2
by: Mark Rae | last post by:
Hi, This isn't *specifically* an ASP.NET question, so I've also posted it in the ADO.NET group - however, it's not too far off-topic... Imagine a SQL Server 2005 database with a table with an...
29
by: Carl Banks | last post by:
Anyone with me here? (I know the deadline for P3 PEPs has passed; this is just talk.) Not many people are bit-fiddling these days. One of the main uses of bit fields is flags, but that's not...
16
by: Santhosh | last post by:
Hi to all, How the individual digits of a number can be obtained using the bitwise operators alone.Is it possible to do it ? If we have n = 34 Result has to be 3,4. Thanks a billion for...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.