Bitwise Operators - C Programming
I am reading K&R, and I don't exactly under stand the |, ^, &, and ~. Are these entirely necessary to know? How often do I need to know these?
|
When I was working through K&R, bitwise operators gave me some trouble too. When I'd sorted it all out, I wrote this article as a summary. You might find it of some use..
|
You'll need to use these basically anytime you start dealing with raw bits. In C, this can happen a lot, depending on what you're doing. Maybe you want to check some data that's come over the network to find out if it has a flag set -- use bitwise AND. Or you're passing some options to an OpenGL function -- use bitwise OR. It comes up a lot, actually, if you start doing such low-level things. And if you're not doing low-level things... well, what are you using C for?! :)
|
Quote:
Code:
unsigned int a = 42; Code:
In binary, a = 101010 |
Quote:
osor: That was also helpful. Thanks for the fast and helpful replies. |
Just so that you know your efforts to understand bitwise operators are not wasted, they are implemented in most other languages as well :).
|
All times are GMT -5. The time now is 01:30 PM. |