Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Two values in one variable

Name: Anonymous 2010-01-16 18:52

Let's say you have two small values that will never get past 10, and you use the first four bits of a char for the first var and the last bits for the second var. Do people do that?

Name: Anonymous 2010-01-16 18:54

i do.
but usually only for flag bits

Name: Anonymous 2010-01-16 18:55

I can't say that I've never done it, but as a rule I don't.

Name: Anonymous 2010-01-16 18:56

>>1
Not generally if they have any sense. If you're out of memory and it's not at all speed-critical, do it. If it's even slightly speed-critical, optimize your memory use instead. If you're programming an actual computer, don't do it. You're just making your program more complicated for no reason, and potentially slower if these are accessed at all in your inner loops.

Name: Anonymous 2010-01-16 19:09

>>4
If it's even slightly speed-critical, optimize your memory use instead.
How would you do that then? It makes sense to me to use bitflags when, say, programming a game for an embedded system such as the DS, but pretty much everything is speed-critical then.

Name: Anonymous 2010-01-16 19:13

>>4
That's not optimizing for memory use. It's optimizing for stupidity, unless you're going to have a lot of these variables around (as in a huge array and/or dynamically allocated), as the code to access them this way will easily outweigh the space savings. (hint: a shift+mask will cost you at least 4, maybe 8 bytes on code size; also reducing an int to byte may very well be useless depending on alignment/padding constraints, so you have to know what layout stuff will actually have in memory)

tl;dr: it you have to ask, it's a bad idea for you to do it

Name: >>5 2010-01-16 19:22

>>6
Don't be hatin' on me :(

Name: Anonymous 2010-01-16 19:40

>>6
It is not uncommon to have orders of magnitude more ROM than RAM., e.g. 256B RAM and 4KiB ROM, or 8KiB RAM and 64KiB ROM.

Name: Anonymous 2010-01-16 19:45

In embedded devices,I often find it helpful to do this when outputting data on a parallel port. Viva la bitfields!

Name: Anonymous 2010-01-16 19:48

Yes, hardware registers are commonly like that.

Also, text foreground/background attribute bytes in the 80x25 16-color text modes (I wonder how many on here are old enough to remember.)

Name: Anonymous 2010-01-16 19:59

>>10
I even remember that the high bit of the background was the blinking attribute, so you couldn't have bright background colors.

Or could you? Maybe there was a special switch which disabled blinking, I think I saw some program that did it.

Also I remember programs who used custom characters to simulate widgets such as radio buttons. Some even had mouse cursors in text mode with one pixel granularity; they did this creating 4 custom characters which were the ones that fell under the cursor at any given time (the cursor had the size of one character, 9x16 pixels I think for 80x25) with the corresponding parts of the cursor overimposed. If you moved it over a color difference you could see the underlying boundaries.

Awesome stuff.

Of course this falls on the "huge array" case, still pointless to do for single variables.

Name: Anonymous 2010-01-16 20:07

>>11
blinking attribute
So you could have Web 1.1 in your terminal?

Name: Anonymous 2010-01-16 20:44

>>12
The old Hercules monochrome adapters also had underline, so a convincing Web 1.1 was certainly possible.

Name: Anonymous 2010-01-16 23:46

For the record, are we talking about the same thing as when using the bitwise OR operator?

Name: Anonymous 2010-01-17 0:49

#define GET_BIT(var,n) (var &= 1<<n)
#define SET_BIT(var,n) (var |= 1<<n)
#define CLEAR_BIT(var,n) (var &= ~(1<<n))

Name: Anonymous 2010-01-17 1:30

>>15
#define GET_BIT(var,n) (var &= 1<<n)
wut?

Name: Anonymous 2010-01-17 4:02

>>15
ever heard of C99 bitfields, bru

Name: Anonymous 2010-01-17 4:23

>>1
#include <complex.h>
That's the first thing I thought of when I read the subject.

>>5
I find the DS in particular to be pretty exceptional in this area, unless you're trying to do something like decode video or use an SDL port. The graphics is all done through hardware acceleration. The only time I ran up against the CPU was when doing color filtering in framebuffer mode, but I'm guessing that was probably bottlenecked by memory access in actuality.

Name: Anonymous 2010-01-17 5:47

>>15
wut?

Name: Anonymous 2010-01-17 6:37

>>1
Rarely worth the effort. You should only do this if you're designing hardware or programming some very limited microcontroller. Another possible usage for this is when you're designing some data type that needs to be as short as possible for some reason, for example a serial number that many people should type - in that case it might be worth tightly packing the data. Other usages of tight packing of data are some multimedia formats, but you only do that if there's enough hardware support for efficient processing, and there's a lot of data. Usage scenarios:
1) Hardware design, or embedded programming
2) Data which needs to be entered often by humans.
3) In-memory format of some data if the hardware allows you to efficiently operate with it.
4) Bitfields

Name: Anonymous 2010-01-17 8:38

Packed BCD format is still used in some cases due to the ease of converting to ASCII, and the digits can be treated like separate variables.

Name: Anonymous 2010-01-17 9:23

union u_gfagt
  {
      unsigned char g : 4;
      unsigned char t : 4;
  };

Name: Anonymous 2010-01-17 10:36

>>22
Implementation dependent and therefore mostly useless, everybody does it manually.

You are now hating C manually.

Name: Anonymous 2010-01-17 14:19

>>6
alignment/padding constraints
If I understand correctly, this makes a struct like
struct s {
    char a, b;
    int z;
};

be represented on a 32-bit system as
[ a :8 ][ b :8 ][ padding : 16 ][             z:32             ]
?

Name: Anonymous 2010-01-17 17:51

>>24
Generally yes, and it can get uglier too. Also, there are ways to tell the compiler to forgo alignment. But that's what you'll generally get, 32-bit system or not 32-bit system (on a 64 bit system you'd get the same if int is 32 bits wide).

A shitty but very portable way to check alignment is to get pointers to the struct members and subtract the addresses.

Also note that arrays can have additional padding between elements, and that malloc() and friends usually return more memory than requested (if you request 1 byte it's very possible that you'll get 12, plus more is wasted for management).

Name: Anonymous 2011-02-03 1:02

Name: Anonymous 2011-02-04 13:23

Don't change these.
Name: Email:
Entire Thread Thread List