Sign in to follow this  
Elwren

Bitwise Operators

Recommended Posts

Elwren    122
Hey, I'm a little confused on what data types you can use bitwise operators (specifically the & operator) on. I've use the & operator before on ints and unsigned ints (32 and 16 bit data types) and it worked fine in C. Howeverin C it did not work with 64 bit data types (such as a double). Now I'm using C# and it seems it will only take a regular 32 bit int for these operators. I checked MSDN and either I'm stupid or I couldn't decipher an accurate answer on this. It seems C# will only take ints (not ushort values) whereas C can take both (int and unsigned int) values. Am I making sense or just continually babbling? Could somebody explain a little what values these operators can take in. Thanks a lot.

Share this post


Link to post
Share on other sites
doynax    850
In C you can use them on any integer types (so no floats or pointers).
I don't know what rules apply to C# but I can't see any technical reasons to restrict them on integers.

Share this post


Link to post
Share on other sites
vEEcEE    175
I haven't been coding in C# for a while, but I know for sure you can use bitwise operator on the byte data type (which makes sense). Here's some code I made to convert a byte to a binary string...


private string Byte2Binary(byte b)
{
string s = "";
byte k = 1;

while (k > 0)
{
s = ((b & k) > 0 ) ? "1" + s : "0" + s;
k <<= 1;
}

return s;
}




...works fine. I don't see why it wouldn't work on ushorts. Maybe try putting parenthesis around it like if yer doing something like this:

if ( ((i+1) & 15) == 0 )

Share this post


Link to post
Share on other sites
Endar    668
Quote:
Original post by doynax
In C you can use them on any integer types (so no floats or pointers).
I don't know what rules apply to C# but I can't see any technical reasons to restrict them on integers.


I was sure that you were wrong, but, I just tested it myself. Why is that? Why can't you use bitwise operators on pointers or non-integer types?

Share this post


Link to post
Share on other sites
Sneftel    1788
Quote:
Original post by Endar
Quote:
Original post by doynax
In C you can use them on any integer types (so no floats or pointers).
I don't know what rules apply to C# but I can't see any technical reasons to restrict them on integers.


I was sure that you were wrong, but, I just tested it myself. Why is that? Why can't you use bitwise operators on pointers or non-integer types?

There's two things that bitwise operators could do when applied to floats:

1. Act like integers (5.0 & 3.0 == 1.0). This goes against the original purpose of the bitwise operators, namely to expose operations that ALUs tended to be good at directly in C to improve performance. Also, it's unclear how to extend them to decimals.

2. Work directly on the bit patterns. This, of course, would produce utterly bizarre results, but you should expect that when doing bitwise ops. Further, however, keep in mind that bitwise ops are supposed to be low-level. Floating-point types are not necessarily bitwise-operable-upon by the ALU. Consider, for example, a 64-bit float on an otherwise 32-bit architecture. C/C++ will happily perform bitwise ops on floats for you as long as you reinterpret them as ints first.

Share this post


Link to post
Share on other sites
JohnBolton    1372
Logically, it doesn't make sense for bit-wise operations to work on ints and not floats. But C/C++ has always been close to the hardware and FPUs don't have bit-wise instructions, so that's how it came out.

Share this post


Link to post
Share on other sites
Sharlin    864
Well, IMHO it makes lot of sense. Because the way floating point numbers are stored is not defined in the standard, you couldn't do anything meaningful with bitwise operations on floats and expect it to be portable. Anyways, it is of course possible in C and C++ to apply bitwise operations on the raw bit representation of floats, you just need to coerce the type:

float f = 1.f;
f = *(int *)&f & 15;
// or in C++:
f = (int &)f & 15;

Share this post


Link to post
Share on other sites
Elwren    122
Ya well either I'm still being stupid or bitwise operators do not work on ushorts in C#. I always get an error on the following code:


using System;

class ReverseBytes
{
public ushort setByte(byte source, ushort dest, int byteNum) {
ushort mask;
if(byteNum == 0) {
mask = 0xFF00;
dest = dest & mask;
dest = dest | source;
}
else if(byteNum == 1) {
mask = 0x00FF;
dest = dest & mask;
source = source &lt;&lt; 8;
dest = dest | source;
}
else
exit(1);

returnd dest;
}

static void Main(string[] args) {
ushort testValue = 310;
byte b1 = 54;
byte b2 249;
Sytem.WriteLine("Old value is {0}", testValue);
testValue = setByte(b2, testValue, 0);
testValue = setByte(b1, testValue, 1);
System.WriteLine("New value is {0}", testValue);
}
}



The error is CS0266:Cannot implicitly convert type 'int' to 'ushort'. An explicit conversion exists (are you missing a cast?).

Do I have to always cast them as ints to get it to work? It doesn't make sense to me why they would restrict bitwise operators to just regular integers in C# either. Maybe its just my C# skills are not up to par as I am just starting to learn it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this