I propose this patch on top of Adam's v5. Also included is a full patch against master.
I have attached an updated patch for review (role-attribute-bitmask-v7.patch).
This patch incorporates the 'v5a' patch proposed by Alvaro, input validation (Assert) check in 'check_role_attribute' and the documentation updates requested by Stephen.