12,454,164 members (57,168 online)
Rate this:
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
}
else
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!

Glenn
(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....
Glenn
Posted 14-Jan-13 7:35am
Updated 14-Jan-13 22:36pm
v2
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:

## Solution 1

In this code, there is nothing with resembles bit operations per se. Refer to the operator list:
http://msdn.microsoft.com/en-us/library/6a71f45d%28v=vs.110%29.aspx[^].

You will need binary shift '>>', '<<', binary AND, OR, XOR and NOT ('&', '|', '^', '~'). See also:
http://blog.typps.com/2007/10/bitwise-operators-in-c-or-xor-and-not.html[^].

—SA
Rate this:

## Solution 2

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

```public enum BitFlag : byte
{
First=1,
Second=2,
Third=4,
Fourth=8
}

public static string CheckFlag()
{
BitFlag bFlag = BitFlag.First | BitFlag.Third;
StringBuilder result = new StringBuilder();

if ((bFlag & BitFlag.First) == BitFlag.First)
result.AppendLine("First");
if ((bFlag & BitFlag.Second) == BitFlag.Second)
result.AppendLine("Second");
if ((bFlag & BitFlag.Third) == BitFlag.Third)
result.AppendLine("Third");
if ((bFlag & BitFlag.Fourth) == BitFlag.Fourth)
result.AppendLine("Fourth");
return result.ToString();
}```
v3
Andreas Gieriet 14-Jan-13 20:06pm

Typo: it should be `First = 1`?
Cheers
Andi
BC @ CV 15-Jan-13 9:42am

Rate this:

## 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.
Andi
Rate this:

## Solution 4

Hi.

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.

Regards,
Daniele.

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

Top Experts
Last 24hrsThis month
 Maciej Los 235 OriginalGriff 210 Dave Kreskowiak 115 Bernhard Hiller 80 Member 12658776 80
 OriginalGriff 6,373 ppolymorphe 3,878 Karthik Bangalore 3,635 Maciej Los 3,480 Richard Deeming 2,835

Advertise | Privacy | Mobile
Web01 | 2.8.160826.1 | Last Updated 15 Jan 2013