locked
How to use Hexadecimal words as binary bits like a Register? RRS feed

  • Question

  • User2142845853 posted

    The goal is to use hex to create a value that can be used at the bit level in c# but what is the calling convention?

    if the value is 0x3500 how to AND the specific bits to get a true/false?  

    0     0     1       1      0       1     0     1

    How to AND with the 3rd bit from the left?  if(0x3500 && 0x20) ?  But I want to name each bit, xxl, xl, l, m, s, xs or whatever

    'xxl' EQU 0x8000;  ? 

    if(0x3500 && 'xxl') { ...something }  or Uint32 MyRegister  += 0x3500;   if(MyRegister && xxl) {...  something }   Whats the accepted way to do register/bit manipulation in c#?

    Wednesday, August 28, 2019 1:00 PM

All replies

  • User475983607 posted

    The goal is to use hex to create a value that can be used at the bit level in c# but what is the calling convention?

    Hex is a base 16 number system but a number nonetheless.  For example 0x0A and 10 are equal.

    if the value is 0x3500 how to AND the specific bits to get a true/false?  

    0     0     1       1      0       1     0     1

    How to AND with the 3rd bit from the left?  if(0x3500 && 0x20) ?  But I want to name each bit, xxl, xl, l, m, s, xs or whatever

    I think you are asking how to test a bit using logical AND?

    0011 0101 (0x35)
    0010 0000 (0x20)
    ---------
    0010 0000 (0x20)
    

    In C# these operations are equal.

    int results = 0;
    results = 0x35 & 0x20;
    Console.WriteLine(results);
    
    results = 53 & 32;
    Console.WriteLine(results);

    if(0x3500 && 'xxl') { ...something }  or Uint32 MyRegister  += 0x3500;   if(MyRegister && xxl) {...  something }   Whats the accepted way to do register/bit manipulation in c#?

    Your description and problem is a little confusing as you are mixing Boolean with logical operators.  Anyway, Boolean and logical operators are covered in the C# programming guide.

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/boolean-logical-operators

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/bitwise-and-shift-operators

    Wednesday, August 28, 2019 4:55 PM
  • User2142845853 posted

    The question is for ANYONE OUT THERE who has worked with BASE16/Hex or Hexadecimal and Registers.  So far there has been no answer.

    C# and VS appear to be handicapped and not able to deal with hex values.  we are comparing 2 values in BASE16, prefix is 0x, so some value is sent and each bit needs to see if its corresponding bit is true or false.

    portAbit1 = 0x8000

    portAbit2 = 0x4000

    portAbit3 = 0x2000

    then the register value is passed in as hex, say its 0xC509.  Which bits match?  VS puts the value as decimal, it will not keep hex.  But the goal is to take that raw binary value expressed in hex and compare bit by bit to see if there is a match

    0xC509 & 0x8000  = true

    0xC509 & 0x2000 = false

    If one method or function sends hex out to another function, its seen as decimal, then it has to be converted to string with format'X' so that its back in hex form, but now its a string...   the goal is to make that hex represented string into hex again so it can be tested

    Question: Can the code be converted to ASM so that the bit manipulation can take place directly?   Otherwise its string to hex, then compare

    Thursday, August 29, 2019 6:43 PM
  • User475983607 posted

    rogersbr

    and I want to NAME THE BITS and use the name to and, but I was using the wrong syntax, can probably work around the shortcoming in VS just using hex

    Can you clarify the short coming?  The [Flag] Enum works great for testing bits.

    rogersbr

    So obviously its for a transistor Q1 with various voltages that have to be set, then tests done, 

    foreach(bit x in Q1) {

    }

    I'll need a bit more clarification here too.  My best guess is you have a 24-bit number and want to find the "ones" and "zeros".   Test then shift the bits either left (or right).  A foreach construct is intended to loop over a collection of items not a single value.  

    long num = 0x55555555;
    for(int i = 0; i < 24; i++)
    {
        Console.WriteLine($"bit({i}) = {num & 0x01}");
        num = num >> 1;
    }

    If you want to use foreach, create an array of values 1, 2, 4, etc.  Then you can loop over the values and logical AND the values to the 24-bit number being tested.  

    List<long> Flags = new List<long>();
    long aBit = 1;
    for (int i = 0; i < 24; i++)
    {
        Flags.Add(aBit);
        aBit = aBit << 1;
    }
    
    int counter = 0;
    foreach(long l in Flags)
    {
        Console.WriteLine($"bit({counter++}) = {(num & l) / l}");
    }

    And

    foreach (Q1 q in (Q1[])Enum.GetValues(typeof(Q1)))
    {
        Console.WriteLine($"bit({q}) = {(num & (long)q) / (long)q}");
    }

    And forgot... you're enum is incorrect.

        [Flags]
        enum Q1
        {
            Q1v1 = 1,
            Q1v2 = 1 << 1,
            Q1v3 = 1 << 2,
            Q1v4 = 1 << 3,
            Q1v5 = 1 << 4,
            Q1v6 = 1 << 5,
            Q1v7 = 1 << 6,
            Q1v8 = 1 << 7,
            Q1v9 = 1 << 8,
            Q1v10 = 1 << 9,
            Q1v11 = 1 << 10,
            Q1v12 = 1 << 11,
            Q1v13 = 1 << 12,
            Q1v14 = 1 << 13,
            Q1v15 = 1 << 14,
            Q1v16 = 1 << 15,
            Q1v17 = 1 << 16,
            Q1v18 = 1 << 17,
            Q1v19 = 1 << 18,
            Q1v20 = 1 << 19,
            Q1v21 = 1 << 20,
            Q1v22 = 1 << 21,
            Q1v23 = 1 << 22,
            Q1v24 = 1 << 23
        }

    Thursday, August 29, 2019 8:19 PM
  • User2142845853 posted

    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?

    And the dotnet used here will not allow a cast in binary, doesnt understand binary; .NET v7 now has a 0b_1111_0000

    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?

    Visual Studio has a severe shortcoming trying to handle Hexadecimal literals or I dont know what mysterious hoops it wants me to jump thru

    Good God.  Just pass a HEX VALUE to some private function then do some BIT LEVEL COMPARISON?  possible?

    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

    Friday, August 30, 2019 6:18 PM
  • User475983607 posted

    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)}");

    Friday, August 30, 2019 8:14 PM
  • User2142845853 posted

    showed that 

    ncoming value Trbits:  3758096384

    new hex value tbits: 237700211588   same exact number? are they the same point on the line from zero?   No and you dont get it. but Im not surprised.

    Friday, August 30, 2019 9:31 PM
  • User2142845853 posted
    long num = 0b_0010;
    long bitToSet = 0b_0100;

    Doesnt even compile. The name b_0010 doesnt exist in the current context. VS version used cant handle binary


    Friday, August 30, 2019 9:39 PM
  • User2142845853 posted

    you said my enum was no good, wrong etc.   Asked how?  oh uhh well an enum works fine.  Why did you say what I posted was wrong?  because ur just now discovering about enums and binary values? just realized what hexadecimal is? just another case where what you posted in an answer was totally wrong, useless and off topic.  just posting random nonsense for parody reasons.

    Am hoping for anyone who has experience with hex/binary programming and c# to chime in with an answer or comment, so far nobody has posted anything related to the question or topic, which is still open.  

    Saturday, August 31, 2019 2:20 PM
  • User475983607 posted

    rogersbr

    C# and VS appear to be handicapped and not able to deal with hex values.  we are comparing 2 values in BASE16, prefix is 0x, so some value is sent and each bit needs to see if its corresponding bit is true or false.

    I illustrated how to do this 3 different ways.  

    rogersbr

    C# and VS appear to be handicapped and not able to deal with hex values.  we are comparing 2 values in BASE16, prefix is 0x, so some value is sent and each bit needs to see if its corresponding bit is true or false.

    portAbit1 = 0x8000

    portAbit2 = 0x4000

    VS is not handicapped and testing a bit is very simple.   

    The logical AND result below is zero which means the value being tested does not have a 1 in the bit[1] position.

    0101 (value being tested)
    0010 (test for bit[1])
    -----
    0000

    The following logical AND returns the test nibble which has a value of 2 

    0111 (value being tested)
    0010 (test for bit[1])
    -----
    0010

    Generally this approach is used when the value being tested is encoded meaning that each bit in the value has a meaning.  The [Flag] Enum is a good choice for this type of scenario.  The result of ANDing a Flag with a value is either zero or the Flag value.  That tells you if the bit is a one or not.  In C# it would look like...

                long num = 0b_0111;
                long bitToTest = 0b_0010;
    
                if((num & bitToTest) == 0)
                {
                    Console.WriteLine("Bit not found!");
                }
                else
                {
                    Console.WriteLine("Bit found");
                }

    Another method is shifting and testing the LSB or MSB depending on the shift direction. 

    rogersbr

    you said my enum was no good, wrong etc.   Asked how?  oh uhh well an enum works fine.

    It's not that [Flag] enum are wrong.  You are using the construct incorrectly.  However, if you take a look at my examples you'll see that I show how to loop over enum values and test.

    rogersbr

    because ur just now discovering about enums and binary values? just realized what hexadecimal is? just another case where what you posted in an answer was totally wrong, useless and off topic.  just posting random nonsense for parody reasons.

    That's an interesting point of view.  From my perspective your responses clearly illustrate a lack of fundamental knowledge in this subject.   I have no idea how to help you as you are not at a level to understand the answer. A good example is your code.   Here you are confusing strings with numbers.  Plus you are trying to convert a string in base10 notation to Hex (base16 notation).  The problem is Hex includes every base10 notation 0-9.

    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
    }

    rogersbr

    long num = 0b_0010;
    long bitToSet = 0b_0100;
    
    Doesnt even compile. The name b_0010 doesnt exist in the current context. VS version used cant handle binary
    
    
    

    I assume this is user error.  You'll get that error if you forget the first zero.  Make sure you have 0b_ in front of the binary number.  You can separate each nibble with an underscore to make it a bit more readable.

    Saturday, August 31, 2019 4:03 PM