Take this scenario: you have several flag enumerations in C # related to (and actually generated) Enum-ish tables in SQL Server. Say that you are a distributor, and you allow your resellers to indicate which US states they are shipping to. As a brilliant and elegant software engineer, you implemented them as a bit-combinable flag value to save storage:
create table USState ( StateID bigint, StateAbbr char(2), StateName varchar(50)) create procedure GetStatesByFlag (@StateFlags bigint) as declare @StateIDs table ( StateID bigint, primary key (StateID) ) insert into @StateIDs select StateID from USState where @StateFlags & StateID != 0 or (@StateFlags = 0 and StateID = 0) select s.StateID, s.StateAbbr, s.StateName from USState s join @StateIDs si on si.StateID = s.StateID
Sweet. You can enable / exclude dynamically in both SQL and C # using bitwise logic, which allows you to instantly remove checkboxes and select lists in Asp.NET, while retaining only one 64-bit number to preserve any combination of the selected ones. And you do not need a non-indexable comparison operator in your procedures. WHERE clauses, with the exception of the enumeration table itself, which has a maximum of 64 rows. Finding your distributors for anyone traveling to India and California can still use the equality comparison and index.
Now you have a request to add support for the territories of the United States, postal codes of the armed forces and provinces of Canada and do this in the reverse order. There is no cutting list to <64 entries, and the business really wants to avoid the need to share the old school countries with the rest of the territories and units.
What do you do?
Creative answers are welcome, but the real problem is this: is there a way to get the same bit math that works with unsigned 64-bit values ββto work on signed ones using negative space to exceed 64 possible bits, like in C # , and in SQL (2008)? If it matters, the flag is modeled, not the βrealβ flag, so it is technically not necessary that this works against the CLR with the [Flags] attribute.
source share