On 19 Apr., 10:01, "Hak...@[EMAIL PROTECTED]
" <Hak...@[EMAIL PROTECTED]
> wrote:
> I was asked to produce an M-tree class using unsigned ints in hex to
> traverse through and find nodes. The thing I'm having trouble working
> out is, I was given (on paper) 25E1 & 0010 = 00E0, but when I tried it
> on my computer, I found C++ converting it all to binary and producing
> 0x4 & 0x3 = 100 & 011 = 0.
The operators &, | and ^ have quite low precedence, so maybe you did
not set braces properly?
> Obviously, that's not helpful. How can I take my hex numbers and do
> the & operation? Or maybe my question is wrong--how can I access the
> n'th digit?
Not sure if you mean binary or hex digits, so both versions:
// returns true if the nth binary digit of x is 1, else 0.
bool nthdigit2( unsigned int n, unsigned int x ) {
return ( x & ( 1 << n ) ) != 0;
}
// returns the nth hex digit of x.
unsigned int nthdigit16( unsigned int n, unsigned int x ) {
return ( x >> ( n << 2 ) ) & 0xF;
}
In both cases, numbering starts at 0 for the least significant digit.
nthdigit2( 8, 0xFFFFFEFF ) --> false
nthdigit2( 8, 0x00000100 ) --> true
nthdigit16( 3, 0xDEADBEEF ) --> 0xB
nthdigit16( 0, 0xDEADBEEF ) --> 0xF
best,
Michael.
--
[ See http://www.gotw.ca/resources/clcm.htm
for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


|