Click here to Skip to main content
16,011,626 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Adding hexadecimal values in c++ Pin
Kreatief19-Nov-04 1:18
Kreatief19-Nov-04 1:18 
GeneralEdit box and cursor position Pin
Imtiaz Murtaza19-Nov-04 0:43
Imtiaz Murtaza19-Nov-04 0:43 
GeneralRe: Edit box and cursor position Pin
Steve S19-Nov-04 0:49
Steve S19-Nov-04 0:49 
GeneralRe: Edit box and cursor position Pin
Jack Puppy19-Nov-04 0:49
Jack Puppy19-Nov-04 0:49 
GeneralRe: Edit box and cursor position Pin
22491719-Nov-04 0:57
22491719-Nov-04 0:57 
GeneralSaving variables with odd bit sizes Pin
rikkemus19-Nov-04 0:25
rikkemus19-Nov-04 0:25 
GeneralRe: Saving variables with odd bit sizes Pin
Jack Puppy19-Nov-04 0:32
Jack Puppy19-Nov-04 0:32 
GeneralRe: Saving variables with odd bit sizes Pin
Steve S19-Nov-04 0:43
Steve S19-Nov-04 0:43 
rikkemus wrote:
(Its a school projekt on sound compression.)

That's honest, at least, and more interesting than some of the project/homework questions...

OK. You have a short value (which is normally 16 bits), and you want to save only 11 bits. File writes must be a multiple of 8 bits (1 byte). Therefore, if you have only one short, you lose, since you can't write only 3 bits to a file, and have to write 16 bits in total.

However, if you have two shorts, you want to write 22 bits. You still lose, because you have to write 24 bits, not 22. However, that's better, since you now have only two redundant bits. Your strategy should be to pack the bits into characters for writing, and unpack them after reading.

This requires use of bit shift operators >> and << and the bit manipulation operations, and (&); or (|) and not (~).


If you know how many n-bit values you have then you need to write that out to the file too, so that you know how many to read Smile | :)

After that, you just stream the bits out. There's code that does stuff like this in the zlib/gzip stuff, but that might be a bit complex (I'm not meaning to patronise, but it's not the easiest stuff to understand).

Essentially, you start with an empty buffer. You add to the buffer the bits you want. Let's use 11 as an example. The buffer contains bytes. Lets number the bits from the first 4 values as 1 2 3 4 for illustration. After adding four values, you'd have

11111111
11122222
22222233
33333333
34444444
4444????

You just saved two bytes of storage over writing the short values directly.

You need to track which unused bits there are in the buffer, as on the next write, the buffer should contain

11111111
11122222
22222233
33333333
34444444
44445555
5555555?

and so on. This isn't necessarily the best way to 'compress' the data, since it's only removing redundant bits, and doesn't take advantage of any other encoding the way that something like MP3 does, but it's a start, and better than nothing. You can use something like zlib (in a DLL) and write the data as shorts, and that will probably give you bigger savings, but might not count as a success in your project Smile | :)

Steve S
Developer for hire
GeneralRe: Saving variables with odd bit sizes Pin
rikkemus19-Nov-04 1:16
rikkemus19-Nov-04 1:16 
GeneralRe: Saving variables with odd bit sizes Pin
Bob Flynn19-Nov-04 1:14
Bob Flynn19-Nov-04 1:14 
GeneralRe: Saving variables with odd bit sizes Pin
Henry miller19-Nov-04 2:14
Henry miller19-Nov-04 2:14 
GeneralRe: Saving variables with odd bit sizes Pin
Bob Flynn19-Nov-04 2:34
Bob Flynn19-Nov-04 2:34 
GeneralRe: Saving variables with odd bit sizes Pin
rikkemus19-Nov-04 2:55
rikkemus19-Nov-04 2:55 
GeneralRe: Saving variables with odd bit sizes Pin
Bob Flynn19-Nov-04 3:53
Bob Flynn19-Nov-04 3:53 
GeneralRe: Saving variables with odd bit sizes Pin
Henry miller19-Nov-04 6:03
Henry miller19-Nov-04 6:03 
GeneralRe: Saving variables with odd bit sizes Pin
rikkemus19-Nov-04 12:21
rikkemus19-Nov-04 12:21 
Generalnewbie needs help Pin
marinme19-Nov-04 0:19
marinme19-Nov-04 0:19 
GeneralRe: newbie needs help Pin
Steve S19-Nov-04 0:27
Steve S19-Nov-04 0:27 
GeneralRe: newbie needs help Pin
toxcct19-Nov-04 1:01
toxcct19-Nov-04 1:01 
GeneralRe: newbie needs help Pin
Henry miller19-Nov-04 2:19
Henry miller19-Nov-04 2:19 
GeneralRe: newbie needs help Pin
toxcct19-Nov-04 5:40
toxcct19-Nov-04 5:40 
GeneralRe: newbie needs help Pin
Jeryth2-Feb-05 6:14
Jeryth2-Feb-05 6:14 
GeneralRe: newbie needs help Pin
toxcct3-Feb-05 2:47
toxcct3-Feb-05 2:47 
GeneralDatabase for C++ Pin
Gammenon18-Nov-04 23:14
Gammenon18-Nov-04 23:14 
GeneralRe: Database for C++ Pin
Steve S18-Nov-04 23:44
Steve S18-Nov-04 23:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.