Is it very important to know bitwise operator well?

J

Junmin H.

Hello, all. I never use them when I am programming.
But i have been reading codes that using them.
When should I use them? Why not use char/short/int/long?
Is it very important to know them well? Because it's binary,
I found that it's a little difficult to understand then decimal.

Thanks.

Junmin H.
 
B

borophyll

Hello, all. I never use them when I am programming.
But i have been reading codes that using them.
When should I use them? Why not use char/short/int/long?
Is it very important to know them well? Because it's binary,
I found that it's a little difficult to understand then decimal.

Thanks.

Junmin H.

IMO, it is very important to know them, as you are sure to come across
them again in the future. I think the root of the problem is that you
find it hard to understand binary numbers. If you are going to be a
half-decent programmer (ie be doing it as a profession), I recommend
that you address this issue immediately. Use Google, there are plenty
of resources on the Internet that discuss binary arithmetic. While
you are doing this, find out about octal and hexadecimal numbers are
as well.

Regards,
B.
 
M

Malcolm McLean

Junmin H. said:
Hello, all. I never use them when I am programming.
But i have been reading codes that using them.
When should I use them? Why not use char/short/int/long?
Is it very important to know them well? Because it's binary,
I found that it's a little difficult to understand then decimal.
They are used quite a lot in everyday C programming.

For instance ANDing by a power of 2 minus one eg 00001111, 00000011 can
provide a very cheap modulus operation.
Also, sometimes you need to treat memory as a stream of bits, not a stream
of bytes. This is typical for compression programs, for example. The only
way to extract bits is to use the operators to mask out the bits you don't
want, shift them, and recombine.
They are also not infrequently used to pack two or more pieces of
information into a single variable. Fairly commonly you will see functions
take flags, combined with the OR operator.

Whilst you can do without them, they are extremely useful, and also one of
the fundamental operations the processor can perform. Addition is performed
with a cascade of XOR and other operations, internally, for instance.

01 + 01 = 10 (binary addition)

XOR the lowest bit to get the lowest digit of result.
AND the two bits to get the carry.
Repeat with carry + one bit in the next place to get a half result
Repeat with half result and other bit to get the next digit
OR the two carrys from previous two operations to get the carry.

Repeat steps until you have all the binary digits, plus a carry.
 
F

Flash Gordon

Malcolm McLean wrote, On 13/09/07 08:21:
They are used quite a lot in everyday C programming.

For instance ANDing by a power of 2 minus one eg 00001111, 00000011 can
provide a very cheap modulus operation.

That is a very BAD reason for using them. Use the modulus operator and
let the optimiser worry about the optimisation.
Also, sometimes you need to treat memory as a stream of bits, not a
stream of bytes. This is typical for compression programs, for example.
The only way to extract bits is to use the operators to mask out the
bits you don't want, shift them, and recombine.
They are also not infrequently used to pack two or more pieces of
information into a single variable. Fairly commonly you will see
functions take flags, combined with the OR operator.

The above, however, is more reasonable.
Whilst you can do without them, they are extremely useful, and also one
of the fundamental operations the processor can perform. Addition is
performed with a cascade of XOR and other operations, internally, for
instance.

<snip>

Stating that as absolute truth is incorrect. HW has been done with
NANDs, although I will admit I don't know current practice. See, for
example, http://www.ece.uci.edu/docs/hspice/hspice_2001_2-239.html
The NAND gates might be used to build XOR for some of it, but if the XOR
is built from NAND gates then XOR is *not* being used as a fundamental
operation. Once I knew how to NAND gate was constructed in silicon, but
it is a long time since I did that stuff.
 
J

Justin Spahr-Summers

Malcolm McLean wrote, On 13/09/07 08:21:

That is a very BAD reason for using them. Use the modulus operator and
let the optimiser worry about the optimisation.

If, for instance, the value of the variable being used on the right
side of the modulus operator isn't known at compile-time, it can be a
very good reason for using bitwise AND. Consider a hash table...
modulus generally takes many, many more cycles than a bitwise
operation.
 
F

Flash Gordon

Justin Spahr-Summers wrote, On 15/09/07 01:17:
If, for instance, the value of the variable being used on the right
side of the modulus operator isn't known at compile-time, it can be a
very good reason for using bitwise AND. Consider a hash table...
modulus generally takes many, many more cycles than a bitwise
operation.

There may occasionally be good reasons for doing it. However, most of
the time I see AND used as a modulus operator it is used with a
constant, and those cases are bad because they make the code harder to
read and are more likely to be done incorrectly.
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top