Sign In| Join

Last post Feb 20, 2012 01:30 PM by caponet

Member

4 Points

5 Posts

Feb 20, 2012 11:35 AM|caponet|LINK

Hi,

Could someone help me to understand the logic behind this if statement?

Why the following statement returns true

If (129025 And 2048) = 2048 Then

and the following one returns false

If (129025 And 512) = 512 Then

Thank you

Contributor

6555 Points

1062 Posts

Feb 20, 2012 11:55 AM|Ramesh T|LINK

Hello Mate,

And operator (all logical operators) does bit wise operation, if it used in between integers.

Below is wat exactly u r doing

11111100000000001(129025 ) & 00000100000000000(2048) = 00000100000000000(2048)

and

11111100000000001(129025 ) & 00000001000000000(512) = 00000000000000000(0) which is not equal to 00000001000000000(512)

7311 Points

1131 Posts

Feb 20, 2012 11:58 AM|sandeepmittal11|LINK

The result has to do with binary "AND"

for the first case:

Binary of 129025 : 11111100000000001

Binary of 2048: 00000100000000000

when you "AND" this, you get "00000100000000000" equal to binary of 2048 so you get true.

second case:

Binary of 512 : 00000001000000000

when you "AND" this, you get "00000000000000000" not equal to binary of 512 so you get false.

Feb 20, 2012 01:30 PM|caponet|LINK

Thank you it makes sense

## caponet

Member

4 Points

5 Posts

## Help with if statement

Feb 20, 2012 11:35 AM|caponet|LINK

Hi,

Could someone help me to understand the logic behind this if statement?

Why the following statement returns true

and the following one returns false

Thank you

## Ramesh T

Contributor

6555 Points

1062 Posts

## Re: Help with if statement

Feb 20, 2012 11:55 AM|Ramesh T|LINK

Hello Mate,

And operator (all logical operators) does bit wise operation, if it used in between integers.

Below is wat exactly u r doing

11111100000000001(129025 ) & 00000100000000000(2048) = 00000100000000000(2048)

and

11111100000000001(129025 ) & 00000001000000000(512) = 00000000000000000(0) which is not equal to 00000001000000000(512)

## sandeepmitta...

Contributor

7311 Points

1131 Posts

## Re: Help with if statement

Feb 20, 2012 11:58 AM|sandeepmittal11|LINK

The result has to do with binary "AND"

for the first case:

If (129025 And 2048) = 2048 Then

Binary of 129025 : 11111100000000001

Binary of 2048: 00000100000000000

when you "AND" this, you get "00000100000000000" equal to binary of 2048 so you get true.

second case:

If (129025 And 512) = 512 Then

Binary of 129025 : 11111100000000001

Binary of 512 : 00000001000000000

when you "AND" this, you get "00000000000000000" not equal to binary of 512 so you get false.

Sandeep Mittal | My Blog - IT Developer Zone

## caponet

Member

4 Points

5 Posts

## Re: Help with if statement

Feb 20, 2012 01:30 PM|caponet|LINK

Thank you it makes sense