Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Question of the Day for 20 Apr 2007


Question of the Day for 20 Apr 2007

Author
Message
Site Owners
Site Owners
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10095 Visits: 1
Comments posted to this topic are about the Question of the Day for 20 Apr 2007 posted at http://www.sqlservercentral.com/testcenter/qod.asp?QuestionID=1011.
Rick Townsend
Rick Townsend
Say Hey Kid
Say Hey Kid (694 reputation)Say Hey Kid (694 reputation)Say Hey Kid (694 reputation)Say Hey Kid (694 reputation)Say Hey Kid (694 reputation)Say Hey Kid (694 reputation)Say Hey Kid (694 reputation)Say Hey Kid (694 reputation)

Group: General Forum Members
Points: 694 Visits: 89
The explanation says "This returns a 5...", when in fact the rest of the explanation correctly states that it returns 7.




Rick
townsends.ca
Hugo Kornelis
Hugo Kornelis
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8337 Visits: 11580

The question is okay (though I'd personally prefer to ignore the bitwise operators - too much people with a string backgroun in procedurall programming and a weak level of DBMS skill will want to use them).

But I really don't understand why this question is in the category "Design and Theory". Clearly, it should be in the "T-SQL" category, right?




Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Adrian Hains
Adrian Hains
SSC-Enthusiastic
SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)

Group: General Forum Members
Points: 112 Visits: 261
I'm trying to visualize a life of bitmasks without bitwise operators. I just had to disable bit 1 on a bunch of rows, and then later reenable bit 1. How would you propose doing this without bitwise operators? With them, it was a snap.

If you would suggest changing the schema, that doesn't work for me. I'm dealing with an off the shelf forum application that would be a ton of work to change the database schema for.
Hugo Kornelis
Hugo Kornelis
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8337 Visits: 11580

Hi Adrian,

Try to visualize a life without bitmasks instead.....

Remember the rules for first normal form - every column should only hold atomic values. Clearly, a bit mask violates that rule. If you want to store 8 yes/no values, use 8 columns instead. Define them as bit, if you must (I am not as opposed to the bit datatype as some, but not a big supporter either), and SQL Server will still use only 1 byte to store them. Or store them as CHAR(1) with a CHECK constraint to limit the population to 'Y' of 'N', and gain a lot of clarity at the price a mere 7 extra bytes.




Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Bonnie White
Bonnie White
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 1

What are some instances where a bitmask would be useful?

I've been programming for a while now and never really understood their purpose.

Thanks!

Bonnie


Hugo Kornelis
Hugo Kornelis
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8337 Visits: 11580

Hi Bonnie,

Usage of bitmasks was very common and useful up until the seventies, when memory footprint off applications and data came at a premium. If you have to run a program on your home computer with only 32 KB (yes, KILObytes) of memory -half of which was reserved for video memory, keyboard memory, and firmware-, you get to appreciate the value of combining multiple values in a single bytes. And when data has to be stored on a casette recorder with a recording andd playback speed of 300 bits per second, you get to apppreciate the savings even more.

Same for professional development. Those computers often had a whopping 64 kB to their disposal - still good reason to save a few bytes. Storage was on floppy disks, or, for the lucky, on hard disks. One of the earliest hard disk bragged a storage capacity of 5 MB, and came at a price of $ 50,000. Try telling your boss: "The good news is, I decomposed the bit mask in the main file for orders into seperate fields; the bad news is, we need to add a second 5 MB hard disk..."

In modern computer development, bit masks are still used quite often in low-level languages like assembly language, or C. These languages are commonly used for the most performance-critical bits of applications, and since many processors have instructions in their instruction sets for handling bit masks, using bit masks can yield a performance boost.

But in modern third-and-higher generation languages, bit masks have no place at all. And in databases, they should be outright forbidden.




Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Adrian Hains
Adrian Hains
SSC-Enthusiastic
SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)

Group: General Forum Members
Points: 112 Visits: 261
Hugo,
It sounds like you have the luxury of getting to define your own database schema. In the small company that I work for, the database guy (yours truly) has to support integration of any other software we need that uses the DB so bitmasks are a fact of life.
Hugo Kornelis
Hugo Kornelis
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8337 Visits: 11580

Hi Adrian,

Sometimes I do, sometimes I don't have that luxury. I am well aware of how much some third-party DBs suck, and how awful it can be to be stuck with them.

That makes knowledge of what bitmasks are and how they can be operated a Good Thing for those in a similar position as you currently are - but it still doesn't make it right to advertise the use of bitmasks as if they are a Good Thing in themselves.

(Note - I don't think that the QOTD actually "advertises" the use of bitmasks, but it does teach people about them without the "don't do this unless forced to by crappy third-party software" disclaimer that I think ought to be attached here).




Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Bonnie White
Bonnie White
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 1

Hugo,

Thanks for the quick response and the practical advice!

I thought I may have been missing out on a good shortcut, but I will stay away from them.

Bonnie


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search