473,583 Members | 4,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

bitwise decimal operators

-----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 with 2's complement,
1's complement, or sign-magnitude arithmetic. But the followup
remark is sometimes also made that the choice of arithmetic isn't
completely unconstrained, since the bitwise operators seem to
presume a base-2 machine.

In fact, this is an unnecessarily restrictive argument. As I'll
show, there are valid -- even useful -- interpretations of the
bitwise operators when applied to base-10 numbers. Furthermore,
these interpretations make it easy to perform bitwise arithmetic
on decimal numbers in your head, without performing cumbersome
conversions from decimal to binary and back.

First of all, remember that in the binary case, and even though
we call them "bitwise" operators, it's not necessary to think
of a computer as computing them one bit at a time. A modern
computer, of course, works on several bits in parallel, and if
you write down some binary numbers in hexadecimal, where each
hexadecimal digit -- or "hexit" -- represents exactly 4 bits, you
can fairly easily work a bitwise problem 4 bits or 1 hexit at a
time, if you also learn a bunch of rules such as that E&7 is 6.

In all honesty, though, for hand calculation, this doesn't end up
being any more practical than working in base 2, since base 16
isn't any more familiar to us than base 2 is, and there are far
too many of those inscrutable rules to remember. However,
thinking about the base 16 working of bitwise operators gives us
an important clue: when applying the bitwise operators to decimal
numbers, we're allowed to approach the problem one digit at a
time, without worrying individually about the underlying bits.

We also need to discover a more intuitive "meaning" for those
abstract operators AND, OR, and XOR. Fortunately, this is easily
done. The AND operator generates a 1 bit only when both of its
inputs is 1. That is, in binary,

0 AND 0 is 0
0 AND 1 is 0
1 AND 0 is 0
1 AND 1 is 1

The practical upshot of this is that the AND operation yields
the *smaller* of its two inputs, and this is a much easier rule
for you and me to remember and to apply.

The OR operator generates a 1 bit when either of its inputs
(or both) is 1. If you work through a table like the above,
you will discover that the OR operator yields the *larger* of
its two inputs. So for the two contrasting binary operators with
their inscrutable base-2 truth tables, we have two contrasting
logical operators "minimum" and "maximum" which are much easier
for us to think about. This is a very nice result, and it's far
too regular and orthogonal to be a mere coincidence. In fact,
it's the cornerstone of the method we'll use to simplify the
application of bitwise operators to decimal numbers.

However, we still have to examine the behavior of the XOR, or
"exclusive or", operator. The output of the XOR operator is 1 if
either of its inputs is 1, but *not* both. That is, returning to
the ugly tabular notation, we have:

0 XOR 0 is 0
0 XOR 1 is 1
1 XOR 0 is 1
1 XOR 1 is 0

Examining the digits, we see that the XOR output is always the
difference between the two input bits, that is, the larger minus
the smaller. So when we're trying to understand the operation
of XOR, it will be much easier to think of it in terms of
subtraction.

So now we can put this all together. To work a bitwise problem
in decimal, examine the two numbers by digits, pairwise, one pair
of digits at a time. If you're computing the bitwise OR, take
the larger digit of each pair. If you're computing the bitwise
AND, take the smaller digit. And if you're computing the bitwise
XOR, take the difference between the two digits.

Let's illustrate this with some examples. Suppose we're
computing the bitwise XOR of 22 and 55. At each digit position,
the larger of the two digits is 5, so the bitwise OR is 55.
The smaller digit is 2, so the bitwise AND is 22. And the
difference between the two digits is 3, so the bitwise XOR is 33.

Another example: 35 and 53. At each digit position, the larger
digit is 5, so the bitwise OR is again 55. The smaller digit is
3, so the bitwise AND is 33. The difference between the two
digits is 2, so the bitwise XOR is 22.

Those are pretty trivial examples, and you may be suspicious that
they're contrived, so let's look at a longer one: 4829 and 6670.
To compute the bitwise OR, take the largest digit at each
position. 4 vs. 6 is 6, 8 vs. 6 is 8, 2 vs. 7 is 7, and 9 vs. 0
is 9, so the result is 6879. To compute the bitwise AND, take
the smaller of each pair: 4620. And to compute the exclusive OR,
take the difference between the digits: 6-4 is 2, 8-6 is 2, 7-2
is 5, and 9-0 is 9, so the XOR is 2259. (Check these results
with your C compiler if you like.)

* * *

The fact that the bitwise operators have a natural and
easy-to-compute interpretation for decimal numbers is more than
just an intellectual curiosity. Decimal bitwise operators are
only interesting if they have practical uses. As it turns out,
the interpretations I've described here *do* have practical
uses, and moreover, they're analogous to the traditional uses
which the bitwise operators have, when applied to binary numbers.

For example, consider the problem of extracting certain bits from
a binary number. This is commonly done using a "bitmask" and the
bitwise AND operator. For example, if we perform the operation

110101010 & 000111000

we extract the middle three bits from the binary number on the
left-hand side, discarding the rest. The operation of the base-2
bitwise AND operator is such that wherever there's a 1, the bits
of the other number are passed through, but wherever there's a 0,
any 1 bits in the other number are suppressed, leaving only 0's.
So the bitmask 000111000 selects just the middle 3 bits of the
9-bit field: 000101000.

Extracting digits from decimal numbers is a common problem.
Wouldn't it be nice if we could use "decimal bitmasks", or
"ditmasks", to extract the digits of a decimal number? In fact,
we can, and we'll use the decimal bitwise AND operator to do so.
Remember, the operation of the decimal bitwise AND operator is to
take the smaller of the two digits in each position. So if we
prepare a ditmask consisting of 0's and 9's, we'll get just what
we want. Wherever there's a 0 in the ditmask, that 0 will be
smaller of the two digits, so the corresponding digit in the
other number will be suppressed. Wherever there's a 9 in the
ditmask, the corresponding digit in the other number will either
be smaller, or will be 9, and in either case the corresponding
digit in the other number will be effectively selected.

To see how this works, let's try an example: we'll use the ditmask
090 to select the middle digit from the three-digit number 884.
The rule as I've just described it works, and you can try it with
your C compiler to double check. Another example: the 5-digit
ditmask 00990, to select the tens and hundreds digits. Try it
on the number 781, you get 780. Try it on 43869, you get 00860.
The 9's in the ditmask do not have to be adjacent, any more than
the 1's in a binary bitmask have to be adjacent -- for example,
the ditmask 090090 when applied to the number 471458 selects the
digits 7 and 5, yielding 070050, or when applied to 3821149
selects the 2 and 4 (0020040).

* * *

In conclusion, C's bitwise operators have meaningful
interpretations and work quite well on decimal numbers.
C would be straightforward to implement on a machine using
base-10 integers, using the interpretations I've described here.
Furthermore, since the definitions of the decimal bitwise
operators I've provided are of necessity compatible (at the
numerical level) with the traditional interpretation, and since
the actual number system used by a computer's CPU is effectively
invisible to a portably-written program, a programmer who wishes
to do so can use the decimal interpretations even on a binary
computer. (After all, this makes just as much sense as assuming
that the base-2 integers of a conventional binary computer are
decimal in some other context, e.g. printing them out using %d.)
For example, the ditmask technique for extracting digits from
decimal numbers works just as well on a binary computer as it
would on a decimal computer, as the examples above (if you try
them on your favorite binary computer) all demonstrate.

Steve Summit
sc*@eskimo.com

-----BEGIN PGP SIGNATURE-----
Version: 2.6.3in

iQCSAwUBQk02rd6 sm4I1rmP1AQGP/wPmJ+pS2DM5yxhJ uanfA0qaBOWFdfY WqHp5
A/heFTMzipJEtFDaz XTPlZ4KwR2XNIBv own4dTds3/JT6JYJ5bLAW0qkS 5kmaQjL
cqU0NKJ3Mmvm+dt xd1Y+sLq0bF1vpf OKyhz3z/ZY9oDBXqP5/btRyIn60EkoPVk4
OFw/Rw0=
=OBO0
-----END PGP SIGNATURE-----
Nov 14 '05 #1
2 3456
Steve Summit wrote:
0 XOR 0 is 0
0 XOR 1 is 1
1 XOR 0 is 1
1 XOR 1 is 0

Examining the digits, we see that the XOR output is always the
difference between the two input bits, that is, the larger minus
the smaller. So when we're trying to understand the operation
of XOR, it will be much easier to think of it in terms of
subtraction.


I think of XOR 1 as flip bit and XOR 0 as preserve bit.

unsigned char bit_rev(unsigne d char byte)
{
unsigned hi_mask, lo_mask;

hi_mask = ((unsigned char)-1 >> 1) + 1;
lo_mask = 1;
do {
if (!(byte & hi_mask) != !(byte & lo_mask)) {
byte ^= hi_mask | lo_mask;
}
hi_mask >>= 1;
lo_mask <<= 1;
} while (hi_mask > lo_mask);
return byte;
}

--
pete
Nov 14 '05 #2

In article <d2**********@e skinews.eskimo. com>, sc*@eskimo.com (Steve Summit) writes:

In fact, this is an unnecessarily restrictive argument. As I'll
show, there are valid -- even useful -- interpretations of the
bitwise operators when applied to base-10 numbers.


What, all that fishing and no bites? Well, *I* thought it was
funny, anyway.

--
Michael Wojcik mi************@ microfocus.com
Nov 14 '05 #3

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

Similar topics

9
2523
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 of 'flags' for users on a website. The function below is supposed to be a search function for one of those flags in a particular thing. The...
4
2477
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 used in hardware programming. Are they used very often in routine C/C++ programming, and what for? thanks, MJH
6
8969
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 programming in straight C, the Win32 API sends windows messages to applications with various settings stored in bit fields, in which case changing the...
10
4648
by: Chad | last post by:
For some strange reason, I thought the following would give me a value of 168, not 166. My reasoning was that 678 - 255 - 255 = 168. #include <stdio.h> int main(void) { unsigned long a = 678; unsigned long ff = 255; unsigned long c = a & ff;
5
5773
by: noridotjabi | last post by:
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...
17
3335
by: mdh | last post by:
If one searches the forum for bitwise operators there are numerous questions and very good answers to the perenial question about what these operators do and why we need them. So, having looked at all these, I approached K&R 2.9 headily!! ...but not for long :-( Quote: The bitwise AND operator & is often used to mask off some set of bits;...
17
2351
by: zirconx | last post by:
I'm trying to understand how the bitwise AND can be used. I've read about what it does but am having trouble applying it practically. I'm working on a system that somebody else wrote and they make use of a MODE flag that gets passed in. They then compare the mode flag against a hard coded value using bitwise AND, and then show or don't show...
1
2335
by: Harika | last post by:
Hi all Any one of you would give me small c program using bitwise operators to below given statement. A function setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost n bits of an unsigned char variable y (leaving other bits unchanged). For ex: if x = 10101010 (170 decimal) and...
29
5929
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 often done in Python because of keyword arguments and dicts, which are lot more versatile. Another major use, talking to hardware, is not something...
0
7894
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7825
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8179
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8323
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7933
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8191
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5372
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3816
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1431
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.