tags:

views:

66

answers:

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.

+4  A: 

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!

ysap
Thanks.But can you give me an example so that i can understand more clearly.sry for inconvenience though.I am just learning the programming.
Thej