0

I am attempting to figure out what

 >> 3 

does in the code shown below. Is >> a redirect and if so, then what is 3? Can someone help?

#define BYTESIZE(bitsize)       ((bitsize + 7) >> 3)
18
  • As an aside, the other meaning of >> is called the "extraction operator," not "redirect." Perhaps you were thinking of shell syntax, where > redirects output to a file. Jan 9, 2014 at 21:13
  • @Lotharyx: In shell syntax, >> appends to a file (which has nothing to do with the C operator that happens to look similar). Jan 9, 2014 at 21:15
  • You're right. > by itself overwrites the file. I was just trying to correct the OP's jargon. :-) Jan 9, 2014 at 21:16
  • BTW, the macro definition should parenthesize its arguments to avoid operator precedence problems: #define BYTESIZE(bitsize) (((bitsize) + 7) >> 3) Jan 9, 2014 at 21:16
  • I added the bit-shift tag. @user2936723 I realize you did not know this is what it is but it is the relevant category.
    – Radiodef
    Jan 9, 2014 at 21:16

1 Answer 1

5

>> is the right shift operator. Right-shift takes a binary value and shifts it right by the right-hand operand. For example:

0100 >> 1 == 0010
00010000 >> 4 == 00000001

In decimal terms, this is the same as dividing by powers of two. >> 1 divides by two, >> 2 divides by four, >> 3 divides by 8, etc.

8
  • 3
    The + 7 part is actually interesting too: effectively it rounds up to the nearest multiple of 8. So for example BYTESIZE(14) returns 2.
    – Radiodef
    Jan 9, 2014 at 21:13
  • It's a pretty clever macro, but the OP was only asking about >>. (-: Jan 9, 2014 at 21:13
  • 2
    Yep! Just an addendum.
    – Radiodef
    Jan 9, 2014 at 21:20
  • Nothing especially "clever" about the +7 thing -- it's an old, very well-established idiom.
    – Hot Licks
    Jan 9, 2014 at 22:00
  • @HotLicks I know that, I'm just being polite to Radiodef, as I thought it was worth mentioning.
    – Cloud
    Jan 9, 2014 at 22:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.