Eric
Eric

Reputation: 1472

C# bitwise addition and subtraction like in C

I'm writing up a QuadTree class and I need to include neighbor searches. I'm following along with this paper which includes a C implementation. (I'm basically converting it to C#.) And I'm having trouble because I'm not very good at working with binary numbers. I need to do the following things:

given #100 and #010, combine them to get #110. 1s will never overlap. right now what I do is store 100 and 10, when I add them it gives me 110.

perform left and right binary shifts on numbers like #011. right now I was storing 011 and applying the >> or << operator to it. This gives me completely the wrong number but strangely the logic I used it in to find common quad tree parents was working just fine.

given #011 and #001 sum them to get #100. Right now I'm storing 11 and 1, this obviously doesn't work at all .

Perform bitwise subtraction, same as above but - instead of + :)

perform ANDs, ORs and XORs.

So, right now I'm storing everything as an integer that looks like the binary value, and the math seems to be working for the ANDs, ORs, XORs and shifts. The numbers are crazy in the middle but I get the right answer in the end.

I'm trying an implementation right now where I store the integer value that is the value of the binary number but I have to convert back and forth a lot, this is particularly a pain because I'm converting between an integer representation of the Value of the number and a string representation of the binary number itself. Creating a new string every second line of code is going to be a performance nightmare.

Can you guys recommend a basic strategy for working with binary numbers in C#?

P.S. I don't ever need to see the integer representation of these numbers. I really don't care that #010 == 2, the work I need to do sees it as #010 end of story.

Hopefully this is more clear than my original post!

Upvotes: 1

Views: 4829

Answers (3)

phoog
phoog

Reputation: 43046

I hope this will clear up some confusion:

  • The literal 0x00000001 represents 1 using hexadecimal notation.
  • The literal 1 represents 1 using decimal notation.

Here are some pairs of equivalent literals:

 0x9 ==  9
 0xA == 10
 0xF == 15
0x10 == 16
0x11 == 17
0x1F == 31

The primitive integral data types (byte, sbyte, ushort, short, char, uint, int, ulong, long) are are always stored in the computer's memory in binary format. Here are the above values notated with five bits:

 1 == #00001
 9 == #01001
10 == #01010
15 == #01111
16 == #10000
17 == #10001
31 == #11111

Now, consider:

unsigned int xLeftLocCode = xLocCode - 0x00000001;

Either of the following is equivalent in C#:

uint xLeftLocCode = xLocCode - 1;
uint xLeftLocCode = xLocCode - 0x00000001;

To assign the binary #10 to a uint variable, do this:

uint binaryOneZero = 2;   //or 0x2

Hexadecimal notation is useful for bit manipulation because there is a one-to-one correspondence between hexadecimal and binary digits. More accurately, there is a one-to-four correspondence, as each of the 16 hexadecimal digits represents one of the 16 possible states of four bits:

0 ==  0 == 0000
1 ==  1 == 0001
2 ==  2 == 0010
3 ==  3 == 0011
4 ==  4 == 0100
5 ==  5 == 0101
6 ==  6 == 0110
7 ==  7 == 0111
8 ==  8 == 1000
9 ==  9 == 1001
A == 10 == 1010
B == 11 == 1011
C == 12 == 1100
D == 13 == 1101
E == 14 == 1110
F == 15 == 1111

It's therefore fairly easy to know that 0x700FF, for example, has all the bits of the least significant byte set, none of the next most significant byte, and the lower three bits of the next most significant byte. It's much harder to see that information at a glance if you use the literal 459007 to represent that value.

In conclusion:

Can you guys recommend a basic strategy for working with binary numbers in C#?

Learn the table above, and use hexadecimal literals.

Upvotes: 3

Ken White
Ken White

Reputation: 125671

The line you marked has nothing to do with bitwise anything. It's a straight subtraction of the number 1 (expressed as a hex double word (DWORD), or unsigned long integer (uint in C#).

Upvotes: 1

Henk Holterman
Henk Holterman

Reputation: 273199

why that line of interest does bitwise subtraction in C but integer subtraction in C#.

They should both do the same. Why do you think otherwise? And what do you mean with bitwise subtraction?

I'm storing the binary number 10 as the integer 10, not as the integer 2. I could be doing this all wrong.

Yes, That's wrong. #010 == 2, not 10.

I guess what I looking for is some basic strategy for working with binary numbers in C#.

By and large, those operations should be the same as in C.

Upvotes: 2

Related Questions