473,327 Members | 2,112 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,327 software developers and data experts.

Is it very important to know bitwise operator well?

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.
Sep 13 '07 #1
5 3273
On Sep 13, 2:59 pm, "Junmin H." <tien...@gmail.comwrote:
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.

Sep 13 '07 #2

"Junmin H." <ti*****@gmail.comwrote in message
news:fc**********@aioe.org...
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.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Sep 13 '07 #3
Malcolm McLean wrote, On 13/09/07 08:21:
>
"Junmin H." <ti*****@gmail.comwrote in message
news:fc**********@aioe.org...
>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.
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/h...001_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.
--
Flash Gordon
Sep 13 '07 #4
On Sep 13, 5:07 pm, Flash Gordon <s...@flash-gordon.me.ukwrote:
Malcolm McLean wrote, On 13/09/07 08:21:
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.
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.

Sep 15 '07 #5
Justin Spahr-Summers wrote, On 15/09/07 01:17:
On Sep 13, 5:07 pm, Flash Gordon <s...@flash-gordon.me.ukwrote:
>Malcolm McLean wrote, On 13/09/07 08:21:
>>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.

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.
--
Flash Gordon
Sep 15 '07 #6

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

Similar topics

12
by: sandy_pt_in | last post by:
How to mulitply two integer numbers using bitwise operators in C language.Please reply as early as possible
8
by: Andy | last post by:
I'm currently learning C with K&R and I was going over the section with the Bitwise operators. What exactly are usefull uses for them? Are they really important? thanks in advance.
55
by: Ennixo | last post by:
hi, do you know where i can find some ebooks or websites talking about C# optimisation ? for exemple, i just learned that ++i is faster than i++. i would like to know more about the things...
10
by: David R. | last post by:
I want to do bitwise operation on some large integers. For example, Response.Write CBool(2 AND 2^30) ' returns False Response.Write CBool(2 AND 2^31) ' CRASHED! Looks like the AND...
17
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...
1
by: mweltin | last post by:
Dear list memebers: I have been searching google and various lists but have come up with little that helps me. Perhaps it is my lack of diction that limits my search results. Be that as it may,...
4
by: John Dann | last post by:
I'm trying to generate a 4-byte unsigned integer in VB2005 that encodes a number of bit-level variables. What I need to do is to be able to compute a value for the uint that is based on specific...
8
by: Daniel Gutson | last post by:
Hi, I just wanted to share another library for doing type-safe bitwise operations in C++: http://bitwise-enum.googlecode.com I found it useful, so hopefully it'll be for somebody else as well....
9
by: Ioannis Vranos | last post by:
Well when we have an expression like this: int obj= first | second; Why is this style preferred than the equivalent: int obj= first+ second; ?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.