Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is this doing: "input >> 4 & 0x0F"?

I don't understand what this code is doing at all, could someone please explain it?

long input;  //just here to show the type, assume it has a value stored
unsigned int output( input >> 4 & 0x0F );

Thanks

like image 216
PseudoPsyche Avatar asked Nov 29 '22 14:11

PseudoPsyche


1 Answers

bitshifts the input 4 bits to the right, then masks by the lower 4 bits.

Take this example 16 bit number: (the dots are just for visual separation)

1001.1111.1101.1001 >> 4 = 0000.1001.1111.1101

0000.1001.1111.1101 & 0x0F = 1101 (or 0000.0000.0000.1101 to be more explicit)
like image 177
Matthew Avatar answered Dec 15 '22 04:12

Matthew