1

If I have some array element, how can I get individual numbers from the array element buffer[0]?

For example, suppose I have buffer[0]=0x0605040302, I'd like to first extract 2, then 0, then 6, etc.

6
  • You might want to edit your post. Use blockquote instead of code. Also what data type is buffer? Commented Aug 9, 2010 at 11:56
  • Im surprised this question is upvoted. Commented Aug 9, 2010 at 11:59
  • If i say buffer is unsigned long long int.How would you extract single digit from the array element. Commented Aug 9, 2010 at 12:03
  • @Thej, you can extract single element by masking other elements. Commented Aug 9, 2010 at 12:11
  • 1
    I edited your question a bit for formatting and clarity, but ultimately I think you need to come up with a better title and refine your question a bit. You're looking for masking, and the fact the number is stored in an array has nothing to do with your actual question. Commented Aug 9, 2010 at 12:14

1 Answer 1

4

The array element content is ONE number. You are trying to extract A DIGIT out of it. Look for masking and shifting - the & and >> operators.

EDIT:

A mask is a string of "0"s and "1"s that let you isolate bits of interest out of a number. A mask containing the hex digit 0xF is used to isolate individual hex digits in a number. For example:

num = 0x4321 (= 0100_0011_0010_0001)
mask = 0x00f0 (= 0000_0000_1111_0000)
num & mask = 0x0020 (= 0000_0000_0010_0000)

Shifting a number effectively brings the required bit to a required position in a number. So, shifting a number to the right by n positions will bring bit #n to place #0.

num = 0x4321 (= 0100_0101_0010_0001)
num >> 8 = 0x0043 (= 0000_0000_0100_0011)

Combine the two operations and you have your extracted digit!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.