views:

320

answers:

2

i am studying the mysql certification guide. at the Bit Data Type section, it says

a BIT(4) sores 4 bit per value

and that storage requirement for a BIT(n) column is (n+7)/8. i dont understand this part. shldnt a BIT(4) take up just 4 bits of storage?

+1  A: 

It seems there is an overhead of 7 bits - probably identifying a block of memory as BIT storage.

This 7 bits is added to the number requestedby BIT(n) and the total is divided by 8 to give the number of bytes. The manual defines the (n+7)/8 as BYTES

So 4 bits requires less than 2 bytes. The manual says 'approximately' because it depends whether you talk about whole bytes or fractions.

pavium
+1  A: 

Actually it's a clumsy way to round up the result. What it means is BIT(1) to BIT(8) take 1 byte, BIT(9) to BIT(16) take 2 bytes, etc... There is no 7 bits overhead. Divide the number of bits by 8 and round up the result. BIT(4) will take 1 byte.

Josh Davis
actually, according to excel and the mysql book formula, bit(1-4) takes 1 byte, bit(5-12) 2 bytes, bit(3-20) 3 bytes, which is quite wierd, i think the formula u mean is simply n/8 rounded up? then i think ur method makes sense, but if i were to be take the certification exam, i guess i need to follow the book? and they shld be right? unless theres an errata?
iceangel89
I think that "/8" is an integer division, similar to this one: SELECT (7+7) div 8;
Josh Davis
ok then i guess that clears things up, guess computers stores btyes not bits? or rather anything using part of a byte will be rounded up?
iceangel89
Yes, in fact you never use bits, you always use bytes. You cannot set or retrieve a bit from the memory, you can only read/write bytes even if you only use half of it. You can read more about bits and bytes on Wikipedia: http://en.wikipedia.org/wiki/Byte
Josh Davis