Click here to Skip to main content
15,919,358 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear all,


My requirement is getting string value(deliveryChannel) from database and based on that value my 4 check boxes are working....

Ex: deliveryChannel value like "0000","0001","0100"....

1st position checkbox1
2nd position checkbox2
3rd position checkbox3
4th position checkbox4

Thanks and Regards,
Ibrahim shaik

What I have tried:

My code is:

checkbox1.visible=Convert.ToBoolean(deliveryChannel[0].equal('1'));
checkbox2.visible=Convert.ToBoolean(deliveryChannel[1].equal('1'));
checkbox3.visible=Convert.ToBoolean(deliveryChannel[2].equal('1'));
checkbox4.visible=Convert.ToBoolean(deliveryChannel[3].equal('1'));


Above code is working fine but I want implement above code using enum and bitwise opertors....
Posted
Updated 14-Feb-17 14:34pm
v2
Comments
[no name] 14-Feb-17 12:45pm    
" I want implement above code using enum and bitwise opertors", okay so go ahead and do that. we aren't going to do it for you.
Patrice T 14-Feb-17 14:35pm    
Ok, do it.

1 solution

Hi Ibrahim,

I assume your bit operation works from the right-hand side. I.e., the rightmost digit of the delivery channel refers to the first check box.

Technically, you need to check which bits are ON (one) in the channel string. You don't need any enumerator for this. The simple solution is:
string DeliveryChannel = "0101";
int BinaryDeliveryChannel = Convert.ToInt16(DeliveryChannel, 2);

checkbox1.visible = (1 & BinaryDeliveryChannel) > 0;
checkbox2.visible = (1 & BinaryDeliveryChannel >> 1) > 0;
checkbox3.visible = (1 & BinaryDeliveryChannel >> 2) > 0;
checkbox4.visible = (1 & BinaryDeliveryChannel >> 3) > 0;
What happens here is you are moving your desired bit to the right-hand side and checking if it is ON by bitwise AND-ing with 1.

Your first bit doesn't need a shifting. The operation performs as:
0001 - for 1<br />
0101 - for the channel mask<br />
-----<br />
0001 - Result of the bitwise AND
Thus, the result is one, and would designate the first checkbox ON.

To check the second bit you need to move it one bit right. So 0101 becomes 0010. That is, the desired bit of the delivery channel mask (second from right), moves to the rightmost position. The operation performs as:
0001 - for 1<br />
0010 - for the channel mask right-shifted one bit<br />
-----<br />
0000 - Result of the bitwise AND
Thus, the bitwise AND is zero, and hence would designate that, the second checkbox will not be visible.

And so on... Hope this helps.
 
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