4 ms·
Far as I can tell 'don't use bit fields' is something students learn from their CS professors along with don't use goto, multiple returns, continue statements,
by Gibbon1 4y ago
Far as I can tell 'don't use bit fields' is something students learn from their CS professors along with don't use goto, multiple returns, continue statements, pass structs by value, and always convert to big endian when sending data over a wire.
My experience is C compilers have ways packing and defining the order of bitfields and structs.
- hoseja 4y ago>always convert to big endian when sending data over a wire What is this awful advice. Only convert to big-endian where legacy demands it.
- wongarsu 4y agoMeh. Network byte order is big endian. Otoh close to every device uses little endian internally and the conversion is pointless except for convention. Otoh it doesn't really matter, because the conversion is extremely fast (including hardware support in common processors). Endianness is really perfectly named: a meaningless difference that generations of people fight holy wars over.
- 0x457 4y agoNetwork only applies to information in packet headers. How you actually pack bytes into your packets is entirely up to you and/or the protocol you're using. It was a flip of a coin choice.
- Gibbon1 4y agoThe LoRaWAN standard uses big endian.
- gonzus 4y agoMost, even all of these, sound to me like cargo-culting. I certainly use multiple returns and continue statements all the time. I pass structs by value when I want to stress that they represent a value type that can / should be copied around (for example, a slice). As for the format to send data over a wire, I will either use the ntoh() / hton() macros, or move to a more standard binary format such as protobuffs. The only thing I don't usually run into is the very topic of this article: bit fields.
- mhh__ 4y agoThe (IIRC) ELF specification explicitly recommends against using bitfields because of implementation defined layout. The compilers often barely document exactly how they lay things out.
- stevenhuang 4y agoYeah no, bitfields are notoriously implementation defined. In the embedded world you often have to deal with vendor specific toolchains, and with their finicky compilers, you'd be surprised the weirdness you run into when using bitfields. You will learn to question everything not specifically defined by the c standard.