From: Hakusa on
Maybe this is just my lack of experience with working with hex, but...

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.

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?

NOTE: This thread was created after about an hour or more of research.
Simply pointing me in the right direction would be appreciated.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Alberto Ganesh Barbati on
Hakusa(a)gmail.com ha scritto:
> Maybe this is just my lack of experience with working with hex, but...
>
> 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.

Where do 0x4 and 0x3 come from? I don't understand.

> 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?

The word "bitwise" says it all: it works on bits, so it's more clearly
visible in binary notation. To get the n-th hex digit just use an "f" mask:

0x25e1 & 0x000f == 0x0001
0x25e1 & 0x00f0 == 0x00e0
0x25e1 & 0x0f00 == 0x0500
0x25e1 & 0xf000 == 0x2000

That's because an hexadecimal f is 1111 in binary.

HTH,

Ganesh

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Pavel Minaev on
On Apr 19, 12:01 pm, "Hak...(a)gmail.com" <Hak...(a)gmail.com> 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 information you were given is clearly wrong.

> Obviously, that's not helpful. How can I take my hex numbers and do
> the & operation?

The standard operator& behaves as expected - it is, by definition,
bitwise AND.

> Or maybe my question is wrong--how can I access the
> n'th digit?

A single hex digit exactly corresponds to a group of 4 bits.
Therefore, you can use bitwise operators to extract those, keeping in
mind that 0xF is 0b1111:

0x25E1 & 0x000F -> 0001
0x25E1 & 0x00F0 -> 00E0
0x25E1 & 0x0F00 -> 0500
0x25E1 & 0xF000 -> 2000


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Goedson Paixao on
On 19 abr, 05:01, "Hak...(a)gmail.com" <Hak...(a)gmail.com> wrote:
> Maybe this is just my lack of experience with working with hex, but...
>
> 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

This doesn't look right to me. 0x25E1 & 0x0010 == 0x0
If you want want your result to be 0x00e0, the operation should be
0x25E1 & 0x00f0

> on my computer, I found C++ converting it all to binary and producing
> 0x4 & 0x3 = 100 & 011 = 0.

Maybe you've forgot the initial 0x to indicate you're writing in
hexadecimal. So 25E1 is interpreted as a double and 0010 is
interpreted as an integer number writen in octal.

> 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?

If you want the n'th hexadecimal digit, you want to do:

(number >> (n * 4)) & 0x0f




--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Brendan on
On Apr 19, 1:01 am, "Hak...(a)gmail.com" <Hak...(a)gmail.com> wrote:
> Maybe this is just my lack of experience with working with hex, but...
>
> 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.

That's because 0x4 & 0x3 = 0x0.

& is a bitwise and.

25E1 & 0010 = 00E0

First off, I'm not sure if you are clear on this, but the above isn't
hexadecimal in C++. These are double constants in C++. In mathematical
notation these are equivalent with 25 * 10 ^ 1 and 0 * 10 ^ 0
respectively. Generally, you don't want to do bitwise operations with
floating points, which technically aren't standard in C++ (although
there is a fairly standard IEEE specification that most hardware I've
heard of uses). Anyway, I've never heard of a use for doing bitwise
operations on floats, aside from maybe cheazy XOR encryption.

Do you mean?
0x25E1 & 0x0010 == 0x00E0

In that case, the above equation just isn't true. Bellow is the
correct result:
0x25E1 & 0x0010 == 0x0000

>
> Obviously, that's not helpful. How can I take my hex numbers and do
> the & operation?

Bitwise operations are performed on binary bits because there is no
other sort of bit on a computer.

>Or maybe my question is wrong--how can I access the
> n'th digit?

Ah! You want to access the n'th hexadecimal digit! This is not what
bitwise and (&) does. Please look here to see what bitwise operations
do
http://en.wikipedia.org/wiki/Bitwise_operation

Right shift (>>) your bits into position (be aware that 4 binary
digits, otherwise known as a nibble, corresponds to one hexadecimal
digit). Once the nibble you want is the least significant in your
number, just chop off everything above that nibble with (num & 0xF).

Then (shifted_num & 0xF) will be the value of your hexadecimal digit.

For instance, say I want digit 2 (counting from zero) of:
int x = 0xABCD;
x = x >> 4 * 2; // now x == 0xAB.
x = x & 0xF; // now x == 0xB.

In general, you will avoid mixups like this if you remember that all
numbers are stored as binary on a computer, and that hex constants
like 0x1234 or decimal constants like 1234 are converted to binary
during compilation. Decimal and hexadecimal don't have a separate type
that bitwise operators work differently on.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]