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)
>>
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.
+ 7
part is actually interesting too: effectively it rounds up to the nearest multiple of 8. So for example BYTESIZE(14)
returns 2.
>>
. (-:
Jan 9, 2014 at 21:13
+7
thing -- it's an old, very well-established idiom.
Jan 9, 2014 at 22:00
>>
is called the "extraction operator," not "redirect." Perhaps you were thinking of shell syntax, where>
redirects output to a file.>>
appends to a file (which has nothing to do with the C operator that happens to look similar).>
by itself overwrites the file. I was just trying to correct the OP's jargon. :-)#define BYTESIZE(bitsize) (((bitsize) + 7) >> 3)