You need skill with the various bit operators to pack several fields into a single int and then unpack them again. You do this work with >>>, <<, &, | and ~ . Rarely you might use the signed shift operator >>. For example to extract the low order three bits you mask with binary 00000111, e.g. z = x & 0x07 . To extract bits 4 and 5 you shift and mask, e.g. z = x >>> 4 & 0x03 . To put together a 2-bit x field in bits 4 and 5 and a 3-bit y field in bits 0, 1, 2 you use code like this: z = x << 4 | y . To zero out a the x field you would take the mask for bits 4 and 5, binary 110000, and invert it, and then mask with that, e.g. z &= ~0x30;
// To put together a 2-bit x field in bits 4 and 5 // and a 3-bit y field in low order bits 0, 1, 2 you use code like this: long z = ( x << 3 ) | y;
![]() |
and suggestions to improve this page to Roedy Green : | ||
| Canadian Mind Products | |||
| mindprod.com IP:[65.110.21.43] | |||
| Your face IP:[38.103.63.16] | The information on this page is for non-military use only. | ||
| You are visitor number 14,518. | Military use includes use by defence contractors. | ||
| You can get a fresh copy of this page from: | or possibly from your local J: drive (Java virtual drive/Mindprod website mirror) | ||
| http://mindprod.com/jgloss/masking.html | J:\mindprod\jgloss\masking.html | ||