BITs: string to int

R

refl

how to convert a string having "011" to int ? (and viceversa) Thanks C#
please!
sample "011" I want a three on the int. "111" = 7
can i have logic on bits from an integer where bit 0 and bit 2 from the
int number are on;like
int[0] & int[2] == true
 
F

Family Tree Mike

refl said:
how to convert a string having "011" to int ? (and viceversa) Thanks C#
please!
sample "011" I want a three on the int. "111" = 7
can i have logic on bits from an integer where bit 0 and bit 2 from the
int number are on;like
int[0] & int[2] == true

Look at Convert.ToInt16(string value, int base).
 
R

refl

that works , thanks

how to do the logic on two int

intA = 3 (0011)
intB = 2 (0010)

need to find out what bit is on in both integers on certain bit position

like I would like to ask: do both integers have bit 1 on ?

thanks


Family Tree Mike said:
refl said:
how to convert a string having "011" to int ? (and viceversa) Thanks C#
please!
sample "011" I want a three on the int. "111" = 7
can i have logic on bits from an integer where bit 0 and bit 2 from the
int number are on;like
int[0] & int[2] == true

Look at Convert.ToInt16(string value, int base).
 
J

Jesse Houwing

* refl wrote, On 5-10-2009 22:01:
that works , thanks

how to do the logic on two int

intA = 3 (0011)
intB = 2 (0010)

need to find out what bit is on in both integers on certain bit position

like I would like to ask: do both integers have bit 1 on ?


bool twoInBoth = 3 ^ 2 == 2

should do
thanks


Family Tree Mike said:
refl said:
how to convert a string having "011" to int ? (and viceversa) Thanks C#
please!
sample "011" I want a three on the int. "111" = 7
can i have logic on bits from an integer where bit 0 and bit 2 from the
int number are on;like
int[0]& int[2] == true

Look at Convert.ToInt16(string value, int base).
 
P

Peter Duniho

* refl wrote, On 5-10-2009 22:01:

bool twoInBoth = 3 ^ 2 == 2

should do

Uh. Either you or I are misunderstanding the question.

If you want to know if a particular bit is set in _both_ integers, you
want the & (and) operator, not ^ (xor). 3 ^ 2 == 1, not 2.

Even there, one would also need to & the result with the integer
representing the bit in question:

int a = 7;
int b = 2;
int bit = 1;
int bitMask = 1 << bit;

bool f;

f = (a & b) == bitMask; // false
f = (a & b & bitMask) == bitMask; //true

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top