4 ms·
> C does not allow you to access bits in memory directly. of course it does what are you talking about?
by heywhatupboys 3y ago
> C does not allow you to access bits in memory directly.
of course it does what are you talking about?
- zabzonk 3y agobits are not addressable in C and are thus not directly accessible.
- jacquesm 3y agoThey are also not normally directly addressable by the CPU, you'll have to do some combining and splitting with separate instructions. Some CPUs are better at this than others.
- messe 3y agoI wouldn’t quite count it as bit-addressing, but x86, for example, can load bits directly into the carry flag using the BT instruction which can take a register or memory address as it’s first argument, with the bit being given as the second.
- jacquesm 3y agoThere have been all kinds of variations on that theme. One of the nicest is 'bit test and set' as an atomic instruction, that one enables a whole raft of nice stuff.
- pm215 3y agoTangent: some Arm Cortex-M class CPUs had a feature called "bit-banding" where you could do byte accesses to an area of the address map and the CPU would turn these into bit accesses to a different part of memory. So the alias word at 0x23FFFFFC maps to bit [7] of the byte at of RAM at 0x200FFFFF, for example, and you can do a word write to 0x23FFFFFC to change just that bit 7, saving having to do it by hand (which is particularly awkward if you need to ensure the atomicity of the bit update). https://developer.arm.com/documentation/100165/0201/Programmers-Model/Bit-banding/About-bit-banding https://developer.arm.com/documentation/100165/0201/Programm...
- messe 3y agoNot the commenter you're replying to, but I suspect what they mean is that the C memory model is byte-addressable not bit-addressable. You can't point/refer to a specific bit in memory, instead you have to first read the byte and then select an individual bit using bitwise operations, much like most modern processors.
- heywhatupboys 3y agoThat has nothing to do with the C memory model, but how the CPU is structured. No modern CPU has an interface for bit-address accessing as far as I am aware... C makes no assumptions about the size of a byte
- Kamq 3y agoC doesn't really know about bytes. It has chars, but I believe there are some constraints on char, specifically, they have to be big enough to hold the ASCII charset. (I'm pulling real deep here, someone correct me if I'm wrong)
- nitrix 3y agoC11 3.6p1 byte "addressable unit of data storage large enough to hold any member of the basic character set of the execution environment"
- messe 3y agoIf I remember correctly, it assumes the size of a char is greater than or equal to seven bits, and a char is defined to be the smallest addressable unit. C does not support bit-addressing.
- nitrix 3y agoThe width is defined as CHAR_BIT >= 8 (C11 5.2.4.2.1p1). The size, sizeof (char), is always 1.
- marcus0x62 3y agoI wouldn't consider accessibility (via masking & shifting or struct bit fields) to be the on the same order as the byte-level addressing you get with pointers.