DataType for bit Comparison

Declan_Basile
Declan_Basile used Ask the Experts™
on
I need to store a string of 16 bits in two different tables and do bit operations on the values.  For example, a field in one table might be the value "0101010101010101" and a field in the other table might be "0001100111100010".  In VBA, I'll need to XNOR them together, get the result, and "OR" the result with another string of 16 bits.  What's the best data type to use to store these values?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
For SQL server, use an Integer field.
Jim HornSQL Server Data Dude
Most Valuable Expert 2013
Author of the Year 2015
Commented:
You may have to use a char(16), as numeric fields like int will strip off leading zeros.
SQL Server does not have a data type specifically suited for a string of bits.

OR

Add up their bit values and store the sum as an int.   For example, 0000000000001011 = 8 + 2 + 1 = 11.  Then use one of the SQL Server Bitwise Operators to write expressions for the number.   I had one client that did complex health care management that did this for various illnesses, 1 = Rheumatoid Arthritis, 2 = Hypertension, 4 = Diabetes, you get the idea, where some patients would add up to 1,8376,432.
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and Programming
Top Expert 2015

Commented:
agree with Anders ... data type will not have to be converted to do bitwise math plus Integer takes only 2 bytes (16 bits - just what is needed) to store as opposed to 1+ byte/character for string and then string would have to be cast to a number for bit-math
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Jim HornSQL Server Data Dude
Most Valuable Expert 2013
Author of the Year 2015

Commented:
An int might not work, as inserting 0001100111100010 into an int will strip the leading zeros and only save 1100111100010 (without the first three zeros).  

Unless there's some kind of conversion function to add the leading 0's back if that is necessary expressions.   If it's not necessary for expressions then int works.

And yes saving it as 4 bits is always better then saving it as 17 bits.
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and Programming
Top Expert 2015

Commented:
adding on

bit <> byte

bit = 0 or 1
byte is a "word" or set number of bits -- for example, 8 bits such as 10001010
 
1 byte can often be equated to 8 bits, but that is not always true; it depends on the environment. Byte can be more bits.
~~~
getting back to original question:

since math will be used on data, best to store as a number. Dropping initial zeros for displaying has no impact on numbers that are stored.
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
You answered your own question:

I need to store a string of 16 bits

So text of length 16.

/gustav

Author

Commented:
Is there any way that I could change this SQL Server code to be a loop?

ALTER TRIGGER [dbo].[trigUpdateBitValues] 
ON [dbo].[CodeTrackingReqs] FOR INSERT, UPDATE
AS

	DECLARE @CumulativeSame As Integer
	DECLARE @CumulativeDontCare As Integer
	DECLARE @CodeTrackingReqId As Integer
	DECLARE @Attrib As TinyInt

	SET @CodeTrackingReqId = (SELECT Inserted.CodeTrackingReqId FROM INSERTED)
	SET @CumulativeSame = 0
	SET @CumulativeDontCare = 0

	SET @Attrib = (SELECT Inserted.Attrib1 FROM INSERTED)
	If @Attrib = 1
		SET @CumulativeSame = @CumulativeSame + POWER(2,0)
	If @Attrib = 2
		SET @CumulativeDontCare = @CumulativeDontCare + POWER(2,0)
	
	SET @Attrib = (SELECT Inserted.Attrib2 FROM INSERTED)
	If @Attrib = 1
		SET @CumulativeSame = @CumulativeSame + POWER(2,1)
	If @Attrib = 2
		SET @CumulativeDontCare = @CumulativeDontCare + POWER(2,1)

	SET @Attrib = (SELECT Inserted.Attrib3 FROM INSERTED)
	If @Attrib = 1
		SET @CumulativeSame = @CumulativeSame + POWER(2,2)
	If @Attrib = 2
		SET @CumulativeDontCare = @CumulativeDontCare + POWER(2,2)

	SET @Attrib = (SELECT Inserted.Attrib4 FROM INSERTED)
	If @Attrib = 1
		SET @CumulativeSame = @CumulativeSame + POWER(2,3)
	If @Attrib = 2
		SET @CumulativeDontCare = @CumulativeDontCare + POWER(2,3)

	UPDATE CodeTrackingReqs SET SameMask = @CumulativeSame, DontCareMask = @CumulativeDontCare
		 WHERE CodeTrackingReqId = @CodeTrackingReqId

Open in new window

Jim HornSQL Server Data Dude
Most Valuable Expert 2013
Author of the Year 2015

Commented:
>Is there any way that I could change this SQL Server code to be a loop?
I recommend asking that as a new EE question, and responding to the experts that spent time and effort responding to your original question.
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and Programming
Top Expert 2015

Commented:
> "So text of length 16"

not necessarily. Integer stores 16 bits in Access -- should be good plus it is already a number data type.

Author

Commented:
Thank you everyone for your feedback.  I think integer will work best because I need to do bit operations but don't need to graphically represent the number in binary.
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and Programming
Top Expert 2015

Commented:
you're welcome ~ happy to help

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial