-1

I am attempting to use a bit-field to store a series of counters that are being used to make triangles for graphics, since I only need values 0,1,2 for some of them and dont want to waste memory. What the code should do is start from bf.vertIndex = 0 and then loop through until bf.vertindex = 6 where it will stop running. However the program results in an infinite loop and when I have it print the value of bf.vertIndex as an int it is always = 1. Am I allowed to use bit-fields with these operands? and am I able to add an integer, such as 2, to the value of a given bitfield?

float vertexData[12];
 triInfo bf;
 count = 0;
 bf.trinum = 0;
 bf.addedx = 0;//tracks if the x cord needs to be offset = 1 or 0
 bf.addedy = 0;//same as for the x cord
 bf.vertIndex = 0;
 //this loop should make a triangle
 while (bf.vertIndex < 6){
    //sets cordinates for a vertex
    vertexData[bf.vertIndex] = 500 + width*(bf.addedx);
    vertexData[bf.vertIndex] = 500 + height*(bf.addedy);
    //keeps track of the number of vertices created and checks where if an 
    bf.vertIndex += 2;
    if (bf.vertIndex = 2 && bf.trinum == 0){
        bf.addedy = !bf.addedy;
    }
    if (bf.vertIndex = 4 && bf.trinum == 0){
        bf.addedx = !bf.addedx;
    }
    cout << ((int)bf.vertIndex);

}

this is the way the bit field I used is laid out

struct triInfo
{
    unsigned char trinum:1, addedx :1,addedy:1, vertIndex:5;

};

I saw a tutorial here Youtube where the gentleman making the video added two values together that were stored as chars at the point 10:15 in the video.

  • 2
    `bf.vertIndex = 2` and `bf.vertIndex = 4` are not comparisons. Your compiler should have warned you about this. – user2357112 Oct 02 '17 at 00:53
  • If those counters are used only to *create* graphics (like above the `triInfo bf;` being the single local variable, discarded upon end of the scope), you are saving memory in wrong place (saving couple of bytes from stack usage), in exchange for poor performance and bigger code. Makes me wonder what made you think this is worth it. (it would made lot more sense to save memory on data types, which you keep hold of, and not in single units, but in hundreds/thousands/... of them, then few bytes more of code may be worth it... still profile also performance to find real bottlenecks) – Ped7g Oct 02 '17 at 03:01
  • You mention values 0, 1, 2 — your code for `addedx` and `addedy` seems to only use 0 or 1. Clearly, `vertIndex` uses a bigger range. If you try to store `2` in `bf.trinum`, you are overflowing the 1-bit wide field. Your code doesn't show that, so it may not be a problem and the mention of 2 may be a red herring. But be cautious. You'd need at least 2 bits to handle 0, 1, 2. – Jonathan Leffler Oct 02 '17 at 06:23

1 Answers1

1

You can compare bit fields with <, >, and ==, just like any other integer. But = is an assignment, not a comparison, and you are inadvertently overwriting the value you are trying to compare.

If you are using clang or GCC, you should get in the habit of compiling with -Wall to enable compiler warnings, and then reading the warnings carefully so as to be able to fix the underlying cause. (Other compilers have similar mechanisms; consult the manual.)

rici
  • 234,347
  • 28
  • 237
  • 341
  • Oh, I had completely overlooked those two signs. changing those has stopped the infinite loop. Im using the compiler that Visual Studio uses by default so Ill have to look into that. thank you for quick answer, with a couple other changes it now works as intended – Peter Goldbreaker Oct 02 '17 at 06:00