Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a device which will giv me the feedback in int type

exp:146,33 etc...

is c++ .net provide any build-in function or method to convert them to bit number?i m using windows form application in visual studio 2008.

another question is how can i split a bit number lets said 10001100 to 8 pieces so i can check each bit is turn off or not?
Posted

I believe the ToString method will convert your int to an ASCII or Unicode representation, not a binary equivalent. You could cast your int to another appropriately sized type:

byte - 8-bits
ushort - 16-bits
uint - 32-bits

result = (byte)MyInt;


Finding the value of each bit is simple; use the & operator to AND your value with a constant

if (result & 0x01)
{ bool bit0 = true;}
if (result & 0x02)
{ bool bit1 = true;}
.
.
.
if (result & 0x80)
{ bool bit7 = true;}


It's a brute force method, and you'll end up with 8 different values, but that might be handy in some applications. Before you perform a cast from int to something smaller, be sure to check that the value is in the proper range for the type else you'll get an error. That is, if your int value is 256 or greater, don't try to cast it to a byte type.
 
Share this answer
 
Comments
AspDotNetDev 19-Aug-10 12:33pm    
Hi Roger... just thought I'd clarify a few points. I'm pretty sure the ToString method indicated by the first responder will work, as it looks like an overload is being used and that overload appears to take the number base the number should be serialized as (in the example given, base 2, which is binary). Still, converting to a string and then back is hardly an optimal solution.

Also, your technique will work and it may be quite fast, but the technique I mention will use less code (a for loop that bit shifts and performs the bit mask, then does something with the result, such as serializing it or putting it into an array).

Also, just thought I'd mention that I like your idea of first casting to the shortest type required. Depending on the processor, that may actually speed up the processing of the subsequent bit masks. Points to you.
Hi,

you can convert the integer to a string that holds the binary representation:

int integerValue = 25;
string *bits = Convert::ToString(integerValue,2);


Then to get the single bit you can use the string.substring function:

string *bit = bits->Substring(2, 1);


Then you would only need to check for the content of the string "bit".

Maybe there is a more simple solution.

Best regards,

JF
 
Share this answer
 
v3
Yes, C++ supports bit masking (use the ampersand) a bit shifting (use double greater than or double less than signs). Google those terms (or start here).
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900