473,486 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Determining a set of flags from a ushort

HaLo2FrEeEk
404 Contributor
I have a 16x16 board, each square can either be on or off. There is a system of saving which square is on, using a number which, I believe, represents a set of flags. For example, the number 2 means that square (1,1) will be turned on. 4 means that (1,2) is on, and 6 means that both (1,1) and (1,2) are on. See?

Since there are 16 square in each column, the number goes up to 65536 for single squares, and that means that square (1,16) is on. But let's say that squares (1,1) and (1,16) are both on, the number would be 65538. If all the squares in a row are turned on, the number would be 131070, or all the numbers for each square added together. I put this number in a hex editor as an int32 and looked at the binary representation:

11111110 11111111 00000001 00000000

Bitshifted right by one it looks like a nice set of flags:

11111111 11111111 00000000 00000000

which can nicely be condensed into an unsigned short. My question is how to get the flags out of it. I confirmed that it is, in fact, flags by turning on the the first, third, and fifth squares in the first column and the result was 42, bitshifted right 1 the resulting bits were:

00010101 00000000 00000000 00000000

(I think I might have to make it little endian, though.)

So, how can I get this to work? How can I extract the on/off value of each bit individually?
Jul 3 '10 #1
1 1531
HaLo2FrEeEk
404 Contributor
Nevermind, guys. I figured this out. It was actually quite simple, which is why it was escaping me. Here's the code I used:

Expand|Select|Wrap|Line Numbers
  1. for (int i = 1; i <= 16; i++)
  2. {
  3.     int x = 1;
  4.     ushort and = (ushort)((int)Math.Pow(2, i) >> 1);
  5.     CheckBox box = (CheckBox)Controls["box" + x + "x" + i];
  6.     bool flagSet = ((flags & and) != 0) ? true : false;
  7.  
  8.     box.CheckState = (flagSet) ? CheckState.Indeterminate : CheckState.Unchecked;
  9. }
It works like a dream. Now to expand it to an actual 16x16 grid, instead of only operating on a single column (for testing purposes.)

You might ask yourself, what use does this have? None, I was just interested in learning how it worked : )
Jul 3 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

10
2225
by: Axel Dahmen | last post by:
Hi, I want to start a technical discussion on the fact that C# doesn't define any mathematical operators on other integral types than int. Simple things like the following just aren't possible...
0
1674
by: CTDev Team | last post by:
Hi, We are using Exchange Server 5.5, and have applications written in VB6 and C# that read and process emails. We are experiencing intermittent errors similar to C# Application ...
4
7940
by: JuLiE Dxer | last post by:
Perhaps, I skipped over learning this particular tidbit. I ran across an odd error while trying to add to ushort variables together and assigning it to a 3rd ushort variable. I get this...
2
3613
by: Garrett | last post by:
Need any help in determining which groups have been given security access to a folder. Searched DirectoryServices to no avail... Any Help?
3
25266
by: Peter Aitken | last post by:
I want to invert all the bits (all 0s to 1s, all 1s to 0s) in a type ushort. I though I would use the complement operator ~ but the compiler won't buy it. How can I do this? Thanks, --...
6
4395
by: Robe | last post by:
Hi, I need to convert a variable from uhsort? type to ushort. The problem is that when I pass the variable of type ushort? to a method that use a ushort type in his parameters the compiler...
2
17084
by: Marcelo Muzilli | last post by:
Howdy all, in my program, I have a ushort variable and a string variable and if I try to compile it, I'm receiving this error message: "Cannot convert type 'string' to 'ushort'". How can I...
4
10931
by: rakesh.Mysore | last post by:
HI I have a problem in casting short to ushort in my project. i have a ushort value of 38,143 now it is converted into short value of 27,393 and stored in variable.(when i casted ushort to...
1
1995
by: xlar54 | last post by:
I have some code that ends up looking alot like: ushort x = (ushort)(m + b); whereas both m and b are ushorts also. This is due to the automatic casting of values to int. Some quick...
8
1695
by: Mark Main | last post by:
I just bought Visual Studio 2008, I'm new to C# and trying to learn it. I'm stuck and would appreciate some help. I need to make the fastest code I can to work with a large key (it's 200 bytes...
0
7126
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7330
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5434
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4865
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3070
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.