Ah, the old classic case of I don't understand it, so it must be bad.
Depends on where this code is. Clean code demands that code be at a similar level of abstraction. If you're writing highly tuned code highly dependent on bits, this kind of stuff is fine. If you see this in the middle of an application at a corporation, it's likely inappropriate premature optimization, and a more expressive (although slower) technique is better.
Plus, it probably should be written like byte = (value >> (i * 8)) & 0xff; if you insist on twiddling with bits for performance.
It's interesting to observe the shift (hah) in programming culture where a snippet like this is thought of as bad code.
Depends on where this code is. Clean code demands that code be at a similar level of abstraction. If you're writing highly tuned code highly dependent on bits, this kind of stuff is fine. If you see this in the middle of an application at a corporation, it's likely inappropriate premature optimization, and a more expressive (although slower) technique is better.
Plus, it probably should be written like byte = (value >> (i * 8)) & 0xff; if you insist on twiddling with bits for performance.
I just implemented a bitboard based chess engine that uses recursion to test it's correct. I asked someone to help me debug and by god people get scared by bitwise operators and recursion.
I'm currently doing an internship project and have to emulate memory management from C (preferably as exact as possible) with stack and heap memory, pointers and type conversion in a typescript (react with babylon as rendering)... I'm going insane, but this somehow makes sense....
How long’s it been since you worked with a fresh grad?
Are you saying those degrees aren't taught programming through C++ that is basically C? It's a fairly new initiative to use higher-level languages for introductory coursework, and even if they use real C++ (through the immediate use of things like std::string and std::vector), Boolean algebra and computer architecture courses will cover bitwise operators.
I’m saying that even if they use C++ for their coursework (they’re not, probably Java, maybe Python), they’re not coming out with a ton of knowledge around bit twiddling.
I read that as "imagine transcripting code" in a very ironic voice and spent a few minutes looking for some (post/meta-) ironic joke in this comment only to realize it comes from a bot and I have spent too much time on the internet
This is actually the right way to write this piece of code. (WTF, why!?)
It's fast and it's portable.
(A comment would have been nice though.)
The coder doesn't know whether the architecture they're writing for is 32 or 64 bits but the compiler will. When it gets to this loop, it can unroll it completely. Then all the "shift 0xFF left by i bytes"--
--like, really think that through; why would you ever shift 0xFF around? Code needs a comment!
anyway, it turns into a stack of 4 or 8 static masks and static assignments and oh hey look, this CPU has instructions that reverse the order of bytes in a register and it only takes a couple of clock cycles... Compared to the more readable approach, it could be tens of times faster.
But the compiler doesn't know to use BSWAP unless it's presented with a loop it can unroll, and those loops... well, this is what they look like. With the advantage that if your target arch doesn't have special high-performance byte-ordering ops, your code still works.
...maybe this is why the coder didn't leave a comment.
This is actually the right way to write this piece of code. (WTF, why!?)
Depends on where this code is. Clean code demands that code be at a similar level of abstraction. If you're writing highly tuned code highly dependent on bits, this kind of stuff is fine. If you see this in the middle of an application at a corporation, it's likely inappropriate premature optimization, and a more expressive (although slower) technique is better. Not everything is coded for the Linux kernel.
Plus, it probably should be written like byte = (value >> (i * 8)) & 0xff; if you insist on twiddling with bits for performance.
Another user suggested an improvement in their answer. The OP code is indeed uselessly cryptic in what it does. Admittedly, though, this is probably one the the nicest pieces of code in this subreddit. LOL
Ich think there is a - 1 missing in the second to last line. I mean I don't know the context, but you end up writing to currenIndex + size in the first loop, which usually should be out of bounds.
A bit late, but this code is portable across different architectures, simply memcpy‘ing the buffer causes the buffer being copied in reverse on an architecture with different endianness. I can‘t say how much performance you loose but I‘m certain copying with memcpy and reversing the buffer them is slower than this method. Learn about Bitwise operations, then it will make more sense for you
107
u/Lataero Apr 29 '23
Ah, the old classic case of I don't understand it, so it must be bad.
92
u/[deleted] Apr 29 '23 edited Sep 27 '24
hobbies grey scandalous juggle ad hoc afterthought onerous possessive dazzling unpack
This post was mass deleted and anonymized with Redact
39
u/ScandInBei Apr 29 '23
I would probably have written it like this:
byte = (value >> (i * 8)) & 0xff;
26
u/Hot-Profession4091 Apr 29 '23
I have officially done too much low level bs because I instantly understood what was happening.
11
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Apr 29 '23
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
9
u/naptastic Apr 29 '23
This is actually the right way to write this piece of code. (WTF, why!?)
It's fast and it's portable.
(A comment would have been nice though.)
The coder doesn't know whether the architecture they're writing for is 32 or 64 bits but the compiler will. When it gets to this loop, it can unroll it completely. Then all the "shift
0xFFleft by i bytes"----like, really think that through; why would you ever shift
0xFFaround? Code needs a comment!anyway, it turns into a stack of 4 or 8 static masks and static assignments and oh hey look, this CPU has instructions that reverse the order of bytes in a register and it only takes a couple of clock cycles... Compared to the more readable approach, it could be tens of times faster.
But the compiler doesn't know to use BSWAP unless it's presented with a loop it can unroll, and those loops... well, this is what they look like. With the advantage that if your target arch doesn't have special high-performance byte-ordering ops, your code still works.
...maybe this is why the coder didn't leave a comment.
6
u/Sanduhr32 Apr 29 '23
I think it converts any endian to raw memory storing from least byte to highest byte.
3
u/dwittherford69 Apr 29 '23
What’s wrong with this?
1
u/Marthurio May 12 '23
It's handy to learn how bitwise operations work, but it looks pretty alien until you figure it out.
It's not bad code in an environment where it's expected that people know how to use bitwise operations.
0
u/tomtom070 Apr 29 '23
Ich think there is a - 1 missing in the second to last line. I mean I don't know the context, but you end up writing to currenIndex + size in the first loop, which usually should be out of bounds.
-1
u/CaitaXD Apr 29 '23
can't you just use memcopy with &value