Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: Algorithms
Expertise: Beginner
Dec 4, 1998



Application Security Testing: An Integral Part of DevOps

Extracting bits.

How do I extract the value from a certain number of bits of an unsigned type?

ex: date is stored as unsigned date = 0x26cf;
    bits         meaning
    0-4          Day
    5-8          Month
    9-15         Year (1980-2099)

How do I get the Day, Month and Year?

There are two steps to extracting bit values this way. First, you must shift those bits down to the zero bit so that the value starts with 0. Second, you must clear any bits you are not interested in so you are testing the value of only the intended bits.

Use C's shift operator (>>), and a bitwise AND (&) to clear unwanted bits, to extract the values you want.

nDay = (nDate & 0x1F);
nMonth = ((nDate >> 5) & 0xF);
nYear = 1980 + ((nDate >> 9) & 0x7F);
DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date