Click here to Skip to main content
13,086,662 members (56,573 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi all,

It appears that the binary mucking I did earlier was right (but wrong) it but I
need to go over the binary number as bit field the below code is a rough lash up of how I want the 4 bit values from the Binary number

  char[] Input;
            for (int i = 0; i < 4; i++)
                if (Input[i] > 'Z')
                    Input[i] -= 0x20;  // Convert lower case to upper case
                if ((Input[i] >= '0') && (Input[i] <= '9')) // First check to see if the number is 0 to 9
                    Input[i] -= '0';  //If so, then subtract ascii ‘0’ from it
                else if ((Input[i] >= 'A') && (Input[i] <= 'F'))// is this the hex codes A to F?
                    Input[i] -= 0x37;  // Subtract 55 (0x37) so ascii A becomes 10
                    Input[i] = 0;  // Else invalid so substitute 0

If you & with 0x08 and the result is not 0 then bit 3(4) is set

If you & with 0x04 and the result is not 0 then bit 2(3) is set

If you & with 0x02 and the result is not 0 then bit 1(2) is set

If you & with 0x01 and the result is not 0 then bit 0(1) is set

I want to do the old C route of doing it bit wise (a&&2) etc. Man I have to go home!

(If the question peaks interest I will rephrase and make it cleaner tomorrow)

Umm it has, so rephrase time I have a character between 0 to 15 the code above (sort of) converted it to try to get 0 to 15 value out. I believe there should be a ConvertTo to do this but....
Posted 14-Jan-13 7:35am
Updated 14-Jan-13 22:36pm
Philippe Mori 14-Jan-13 19:00pm
Such low level manipulations generally does not make much sense in C#. You should use existing function to convert case or numbers or to check the category of a character (number, letter, space...)
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

In this code, there is nothing with resembles bit operations per se. Refer to the operator list:[^].

You will need binary shift '>>', '<<', binary AND, OR, XOR and NOT ('&', '|', '^', '~'). See also:[^].

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

On the off hand chance you are refering to checking bit flags you could do this in C#:

public enum BitFlag : byte
        public static string CheckFlag()
            BitFlag bFlag = BitFlag.First | BitFlag.Third;
            StringBuilder result = new StringBuilder();
            if ((bFlag & BitFlag.First) == BitFlag.First)
            if ((bFlag & BitFlag.Second) == BitFlag.Second)
            if ((bFlag & BitFlag.Third) == BitFlag.Third)
            if ((bFlag & BitFlag.Fourth) == BitFlag.Fourth)
            return result.ToString();
Andreas Gieriet 14-Jan-13 20:06pm
Typo: it should be First = 1?
BC @ CV 15-Jan-13 9:42am
Thanks, Correction made.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

What you do is something like
int i = int.TryParse(s, System.Globalization.NumberStyles.AllowHexSpecifier, null, out i)
      ? i
      : 0;

This has nothing to do with bit operators.
Rate this: bad
Please Sign up or sign in to vote.

Solution 4


I agree with Sergey Alexandrovich Kryukov about no bitwise operation can be found in your example.

However, you can rewrite your loop as:
for (int i = 0; i < 4; i++)
    char ch = Input[i];
    ch = char.ToUpperInvariant(ch); // Convert any Lower case to Upper case
    int nValue = "0123456789ABCDEF".IndexOf(ch);
    if (nValue < 0) // -1 means not found: substitute with 0
        nValue = 0;
    Input[i] = nValue;

This applies to raw chars input like your example.

If instead you have a String as input, then you can use solution by Andreas Gieriet.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170813.1 | Last Updated 15 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100