0

One purpose of left shift operator is multiply the left Operand with 2 and right shift for integer Division. Both have also constraints like undefined behaviours Link etc. I do not understand what will be the real use. I am even not confident to use them for Division or multiplication.

Community
  • 1
  • 1
Wafeeq
  • 869
  • 1
  • 16
  • 34
  • [What are bitwise shift (bit-shift) operators and how do they work?](http://stackoverflow.com/q/141525/995714), [How do you set, clear and toggle a single bit in C/C++?](http://stackoverflow.com/q/47981/995714) – phuclv Jul 22 '16 at 08:44
  • The only real use of `shift operators` is to shift bits. Depending upon the problem/requirement they find their use. So understanding what they do can help or can be found handy in implementation. – sameerkn Jul 22 '16 at 08:47
  • Perhaps the real question you want to ask is __What are shift operations useful for__. – mcmlxxxvi Jul 22 '16 at 09:04
  • Shift operators have overload potential in C++. As it appear the post was focused on the basic "shift", post would have benefited with only a C tag. C++ (and their users) encourages more abstraction (where using`>> <<` for `/ *` is bad) and C is typically closer to the metal (where using`>> <<` for `/ *` is is sometimes good). Choose tags wisely. – chux - Reinstate Monica Jul 22 '16 at 12:57

3 Answers3

4

Only beginners would ever use a shift for division or multiplication (including people who are writing software for decades and are still beginners).

Shift operations are for shifting bits. When you want to shift bits you use them. If you don't know what shifting bits means, you don't want to use them.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • 5
    "Only beginners would use a shift for multiplication **today**". In the past compilers weren't nearly as good at optimization, and it could make a very significant difference to performance to make these sort of micro-optimizations. – Martin Bonner supports Monica Jul 22 '16 at 08:45
  • @MartinBonner true. So today is there any difference between dividing by 8 and shift left 3 times for example? – A. Sarid Jul 22 '16 at 08:50
  • "If you don't know what shifting bits means, you don't want to use them". Otherwise it would be rare. It should not be understood as "Forget it if you do not know". – EFenix Jul 22 '16 at 09:03
  • so you are insulting if someone use shift Operator for multiplying or Division. – Wafeeq Jul 22 '16 at 09:23
  • I am surprised to experienced people here who voted up to this answer. `Shift operations are for shifting bits. When you want to shift bits you use them. If you don't know what shifting bits means, you don't want to use them.` it is like if somebody ask what is unconditional the anwer is which is not conditional. as Name says shift Operator are for shifting it is clear from the Name. it does not say anything about my question. I am asking the real use, applications. – Wafeeq Jul 22 '16 at 09:26
  • btw what is the Problem if I use for multiplying or Division? – Wafeeq Jul 22 '16 at 09:28
  • 1
    @GulluButt it's not shameful to be a beginner, so is it really an insult? Seems like an observation to me. If you're surprised by the upvotes, then why do you assume that they were made by *experienced people*? You cannot see who has upvoted, can you? The problem with using shifts for multiplying and division is that it is less expressive. If you want to *multiply*, use the *multiplication* operator and your intent will be clear. If you use shift, then others reading your code will assume that your intention is to shift bits around. Another problem is the potential UB when the type is signed. – eerorika Jul 22 '16 at 09:44
  • 3
    @A.Sarid: If you are operating on negative values, left shift is undefined, divide by 8 *is* defined. If you are operating on positive (or unsigned) values, then there is no difference in behaviour, but there is a difference in what you are implying to a subsequent reader. – Martin Bonner supports Monica Jul 22 '16 at 09:57
2

One real-world use case is in manipulating bitmasks. For example, if I have a bitmask x and I want to set the 7th bit from the right (where I start counting at zero), I could do the following.

x = x | (1 << 7);
merlin2011
  • 71,677
  • 44
  • 195
  • 329
2

They're bitwise operators, so they're used for bitwise operations. Here's a trivial example that shows some bitwise operations that call for bit shifting.

struct color {
    unsigned char r;
    unsigned char g;
    unsigned char b;
};

void setColor(struct color*,int);
int getColor(struct color*);

int main() {
    struct color myColor;
    int color;
    setColor(&myColor,0x00ff00);
    color = getColor(&myColor);
    return 0;
}

void setColor(struct color* color,int rgb) {
    color->r = (rgb>>16)&0xff;
    color->g = (rgb>>8)&0xff;
    color->b = rgb&0xff;
}

int getColor(struct color* color) {
    return color->r<<16|color->g<<8|color->b;
}
kamoroso94
  • 1,713
  • 1
  • 16
  • 19