Colby
Colby

Reputation: 454

Retrieving elements from a bitshift

How would I retrieve "face" from "i"?

int i = (id | (face << 16) | (type << 18))

Thanks!

Upvotes: 0

Views: 61

Answers (2)

user207421
user207421

Reputation: 310859

You wouldn't, unless you can guarantee that it's only 2 bits wide, and that id is only 16 bits wide, in which case all you have to do is right-shift and mask. Is this a trick question?

Upvotes: 1

billjamesdev
billjamesdev

Reputation: 14642

So, given the restriction that id is only 16 bits wide and that face is only 2 bits wide, use:

face = (i >> 16 ) & 3

Upvotes: 1

Related Questions