Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Bit Data Type Storage Space

Tags:

types

mysql

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?

like image 805
iceangel89 Avatar asked Apr 08 '26 09:04

iceangel89


2 Answers

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.

like image 188
Josh Davis Avatar answered Apr 10 '26 03:04

Josh Davis


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.

like image 22
pavium Avatar answered Apr 10 '26 01:04

pavium