Bitwise Shift Operations?

Nov 25, 2013 at 3:14am
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.

http://www.cprogramming.com/tutorial/bitwise_operators.html

Thanks
Nov 25, 2013 at 3:21am
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

Hope this helps.
Nov 25, 2013 at 3:37am
I understand multiplications and the shift operations but I've given an example above. Can you comment on that?
Nov 25, 2013 at 3:40am
Can you explain what you are trying to accomplish with a shifting mask?
Nov 25, 2013 at 4:00am
I did some bit shifting the other day. Consider the struct:
1
2
3
4
struct Color
{
  unsigned char red, green, blue, alpha;
};


Which you want to be able to assign with an enumerator:
1
2
3
4
5
enum {
  Red   = 0xFF0000FF,
  Green = 0x00FF00FF,
  Blue  = 0x0000FFFF
};


With bitshift it is simple:
1
2
3
4
5
6
7
8
9
Color makeColor( const unsigned int code )
{
  Color ret;
  ret.red   = ( code & 0xFF000000 ) >> 24;
  ret.green = ( code & 0x00FF0000 ) >> 16;
  ret.blue  = ( code & 0x0000FF00 ) >> 8;
  ret.alpha = ( code & 0x000000FF );
  return ret;
}


Last edited on Nov 25, 2013 at 4:03am
Nov 25, 2013 at 4:01am
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.)
Nov 25, 2013 at 4:21am
There is nothing obscure about it.

Bits represent a polynomial whose terms are powers of two.
Nov 25, 2013 at 4:30am
Is my assumption, in my first post about shift operations and bit masks correct?
Nov 25, 2013 at 4:43am
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.
Nov 25, 2013 at 3:06pm
You could loop through bits if you want
Topic archived. No new replies allowed.