What does ">>" really do?

Hi, I'm a novice C++ student. My background is mostly C, and matlab.

I'm trying to understand following line of code

s = 555
int i = (s >> 4) & ~(~0 << 3);


Okay, first of all, I am not even sure what (s >> 4) does. So far, I've only seen ">>" used in std::cout and std::cin. I never questioned the use of >> in context of std::cout. But now, I'm really confused what >> really do in C++. Anyone can explain this to me in the most novice language possible? Thanks.



They're bitshifting operators.

>> is a right shift operator.
<< is a left shift operator.

s >> 4 takes the value in 's' and shifts it right 4 bits.

example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
s = 555;  // 555 in decimal
   // in hex, that's 0x22B
   // in binary, that's 001000101011

int foo = s >> 4;

// s >> 0 = 001000101011
// s >> 1 = 000100010101
// s >> 2 = 000010001010
// s >> 3 = 000001000101
// s >> 4 = 000000100010

// 000000100010 == 0x022, or 34 in decimal

// therefore, foo == 34 


The use in cin/cout has a completely different meaning. I think it was a major bonehead move for them to use << and >> for iostream.
The stream insertion operator << (used in std::cout) is different from the bit shift operator (also <<) as you require. The expression you mentioned is regarding bit manipulations (bit shifting, complement, bit and etc).

To know about the Bit shift operations you can visit the following link.

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

The following link has information about the stream operators:
http://www.cplusplus.com/reference/ostream/ostream/operator%3C%3C/
and right he is.
Last edited on
Feel free to ignore LeafyCircuits' post since it is mostly inaccurate. (•‿•)
Thank you so much. Now I see what's going on here. Oh my....I was hoping to skip studying hex and binary stuff. Now I guess I can't.

But seriously, how often do you come across dealing with hex and binary in your programming work?
Depends on what kind of programming you are planning to do.

Reading external nontext files, you will find yourself using hex/binary/bitshift/etc a lot of times.
Last edited on
how often do you come across dealing with hex and binary in your programming work?

Hex - very frequently. Binary less often, but that's partly because translation between hex and binary is pretty straightforward, and hex is easier to deal with, a string of 1s and 0s can be easily mis-read or mis-typed.

To do any serious programming in C or C++, you will need to use a debugger to examine the contents of your variables as the program is executing. Almost immediately, you will be exposed to hexadecimal data. Addresses, such as the contents of a pointer are usually expressed in hex.

If you think that's too heavy-duty, there are lots of more everyday examples. Take this random URL,
    http://en.wikipedia.org/wiki/Earthshaker_%28album%29
notice the %28 and %29 characters - they are hex encoding of a character.

Or say you have a file containing text like this:
"hello" “world”.
You see there are three different symbols used to represent the double quote mark. The easiest way to recognise which is which is to look at the hex values,
" is 22
“ is 93
” is 94

Similarly, distinguishing between various whitespace or non-printable characters may be easier in hex.

Trying to avoid the use of hex is a bit like trying to avoid the use of if-then-else, it's pretty much fundamental to programming. You may not use it every day, but when you do use it, then it gets used a lot.

Last edited on
Chervil, thanks for the information. I'm convinced. I will try to do those exercises.
If you are planning some very high level programming like c# or java you might skip it.

But c and c++ need strong understanding of how variables are stored and represented in memory. By knowing hex and bin number representation it will be easier for you to know what really is happening in your code, not mentioning that it is very crucial for using pointers.

And remember, in computing hex and bin number representation are being used to make things simpler, not harder :)
Topic archived. No new replies allowed.