 • ### Re: How to use Hexadecimal words as binary bits like a Register?

#### rogersbr

it doesnt work if you start with a HEX value, VS will convert it to base10.  If you wanted to bitwise AND 2 hex values? 0x2f4 & 0x1c3 it appears to convert to base 10 then AND's the digits?

Again Hex and base10 are numbers systems.  What you see on the screen is a string that represents the number.  If you want to see Hex then use a standard numeric format.

https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings

`Console.WriteLine(\$"{(0x2f4 & 0x1c3).ToString("X")}");`

Result

`C0`

#### rogersbr

To make it simple and find what kind of Indian it is, I just want to logical AND 0x800000000 and other values. So what does VS need to use hex values? convert it to string and then to hex

```private void setbits(UInt32 Trbits){

string hexValue = Trbits.ToString();

long tbits = long.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);

// we use long because UInt32 overflows and cant handle the conversion
}```

And then? its still put back to base 10 for operations

Incoming value Trbits:  3758096384

new hex value tbits: 237700211588   ...Really? the same number?

No, .NET is understands binary just fine.  You are having a hard time grasping that binary, hex, and base ten are number systems.  The actual numeric values, the magnitude, the distance from zero, are represented in all systems.  The only difference is how the values are represented on the screen or debugger.

#### rogersbr

private void myFUN(uint hexValue)

{

if( 0x8000000 & hexValue)

{   turn light on;  }

}

What is the problem with this paradigm?

You say my Enum is no good, thats fine, can you show how its supposed to be?  binary comparison is whats needed thanks

The problem you are trying to solve is not very clear.  I think you are trying to set a bit? Use a logical OR.

```        public static long SetBit(long num, long bitToSet)
{
return (num | bitToSet);
}```
```long num = 0b_0010;
long bitToSet = 0b_0100;
long result = SetBit(num, bitToSet);

Console.WriteLine(\$"{num.ToString("X")} OR {bitToSet.ToString("X")} = {result.ToString("X")}");```

Result

`2 OR 4 = 6`

#### rogersbr

What is the problem with this paradigm?

You say my Enum is no good, thats fine, can you show how its supposed to be?  binary comparison is whats needed thanks

There's nothing wrong with using a [Flag] Enum.  The main problem is you need to brush up on your binary.

Edit: if you want to see the values as binary.

` Console.WriteLine(\$"{Convert.ToString(num, 2)} OR {Convert.ToString(bitToSet, 2)} = {Convert.ToString(result, 2)}");`