There's allot of information on Bitwise operations on the internet but I think some aspects are really poorly explained.
I'm trying to work out why you would employ a bitwise Shift operation. I understand how it works perfectly,, just not sure why you would bother.
One theory I have is it allows you to create a mask which can change via a loop. Each loop shifts bits one to the left so you can use an OR bitwise operation to manipulate bits at a bit level. However, this doesn't seem to be explained in articles I've read on this topic. Like the one below. Maybe I'm missing a major point? Can anyone explain why you would use shift operations in simple terms.
Manipulating bits is a fundamental operation in computation, not just at the low-level, but in much higher-level mathematics. At the moment, you just don't see any usefulness, because you lack the fundamentals to 'get it' yet.
Don't worry, you'll get there. For now, just remember that it has many, many applications.
For your purposes right now, you can find it useful with:
- bitflags
- multiplication / division / remainder
- sets
I'm not trying to accomplish anything.. I'm trying to understand firstly if my assumption (as in my original question) is correct about shift operations and bit masks. Essentially, I'm trying to establish a link between the millions of web pages and tutorials on bitwise operations such as AND and ORs and the actual Shift operator.
For the rest of my life I many never need it, but I do want to understand it.
I've copied a section from this article (link in previous post) which I've also bolded in the bottom section to highlight a confusing point to me. Why does it suddenly talk about shifting bits when all we are trying to do is determine whether a bit is 1 or 0?
XX0XXXXX &
00100000 =
--------
00000000
So we get a non-zero number if, and only if, the bit we're interested in is a 1.
This procedure works for finding the bit in the nth position. The only thing left to do is to create a number with only the one bit in the correct position turned on. These are just powers of two, so one approach might be to do something like:
1 2 3 4 5 6 7
int is_in_use(int car_num)
{
// pow returns an int, but in_use will also be promoted to an int
// so it doesn't have any effect; we can think of this as an operation
// between chars
return in_use & pow(2, car_num);
}
While this function works, it can be confusing. It obscures the fact that what we want to do is shift a bit over a certain number of places, so that we have a number like 00100000 -- a couple of zeros, a one, and some more zeros. (The one could also be first or last -- 10000000 or 00000001.)
You have made no assumption. You stated a couple facts, and (almost) asked if there was any use for what you listed as possible to do.
So the answer is: Probably not, since you still haven't described what you are trying to accomplish with a shifting bitmask. You can do it, it is true, but unless there is a point in doing it then why do it?
Such a thing might be useful in cryptography. I can't think of any other use.