This page is a mirror of Tepples' nesdev forum mirror (URL TBD).
Last updated on Oct-18-2019 Download

C++: working with bits?

C++: working with bits?
by on (#181124)
Hello,
I'm reading a 6502 programming book, and have decided to code every peice mentioned in the book, put it together and make it work.

so, i've got the boost libraries installed and am using 8 and 16 bit integer types. but i can't measure them( sizeof() only measures in bytes ) and i don't know how i can work with bits if i'm only measuring bytes.

anyone know of a solution to this? bi quyet lam giau

Code:
#include <iostream>
#include <boost/cstdint.hpp>

using namespace std;

int main()
{
   static const boost::int8_t   int8;
   cout << "sizeof(int8) = " << sizeof(int8) << endl;
    return 0;
}
Re: C++: working with bits?
by on (#181128)
I don't think there are any 6502 C compilers powerful enough for C++, much less the highly complicated Boost library.
Re: C++: working with bits?
by on (#181129)
I don't really understand how C++ could have anything to do with a 6502 programming book... How/why do you intend to code things mentioned in a 6502 book in C++?
Re: C++: working with bits?
by on (#181130)
C++ does have bit fields, but whether or not they get stored efficiently is up to the compiler, and that's generally very spotty.
http://en.cppreference.com/w/cpp/language/bit_field

Because the bit field language feature leaves much to be desired, most people seem to pack bits manually, often using macros to make it more convenient.

As for 6502, I don't know much about trying to use C++ for 6502, but macro techniques for bit packing usually still work in C.
Re: C++: working with bits?
by on (#181134)
rainwarrior wrote:
C++ does have bit fields, but whether or not they get stored efficiently is up to the compiler, and that's generally very spotty.
http://en.cppreference.com/w/cpp/language/bit_field

I'm pretty sure those are also available in plain C, though whether or not CC65 supports them is another question.
Re: C++: working with bits?
by on (#181136)
Oh, you're right. They're available in ANSI C. I guess I just assumed they were a later addition because of their obscurity. (I've almost never seen them used.)