Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
😁 Always a pleasure to fork your ✨ solutions, dear...
👍
See my comment on the fork: you will understand why... my old reflex is C-style :-))))) \
Could write it in 6809 8 bit assembly :-)
:-)
You know what ? I'm 60 (yop a gran'pa programmer). Began programming in 1979... with 68000 assembly my boss was a p.. in the ... for ANY SINGLE CPU CYCLE...
Want the whole story: We had to rasterize an A0 plot, black and white. Makes something like 1 or 2 Mpixels. Memory chips were so expensive: my boss gave me a hardware with 256k (yop) of (static) RAM, and a 20Mo (yes, Mo) hard drive and told me: do the shit with what you have :-)
Actually with modern pipelines and cache, had to readapt... Performance is not a matter of cycles, but a matter of data, cache line and prefetch and branch prediction and...
The change to multiplication is brilliant. It's sad that a redundant cast is still needed to avoid the ugly warning
C-style cast makes it 98% close. XD
Your idea with ranges sounds great.
That was my approach too
this is genius!
masterpiece
No, using xor does not make the function redundant.
Great explanation! The cool thing about XOR is that
x ^ x == 0
for any value ofx
. That's what it all comes down to. In particular, it implies that ifa ^ b ^ c == d
thena ^ b ^ d == c
is also true, and similarly for all the other permutations. It may look like magic, but it's just math. :-)Yo thanks I didn't know how to use bins and couldn't find the information online with what I used to search, this will come in really useful
Good point; it's an easily missable diference between C and C++. :)
It looks like most major C++ compilers actually do allow
union
-based type punning as an extension, even though it isn't strictly required.GCC explicitly allows it even when
-fstrict-aliasing
is enabled, as long as you don't access union elements via a pointer.I had trouble finding an official reference on how Clang behaves, but I played around in Compiler Explorer for a bit and both GCC and Clang handled
union
-based punning gracefully even on-O3
. Neither issued any warnings with-Wall -Wextra -Wpedantic
enabled.So it's probably fine to type-pun with unions on the major compilers, but I imagine things could get """fun""" quickly in lesser-used environments.
interestingly,
union
type punning is undefined behavior in C++ but not in CThis solution is pretty slick, but it unfortunately invokes undefined behavior in the two pointer casts due to C++'s type aliasing rules. Essentially, referring to a value of type
T
through a pointer of a different typeU*
is undefined behavior unlessU
is thesigned
/unsigned
equivalent ofT
; or at least one type ischar
,unsigned char
, orstd::byte
.Many compilers are cautious and will try not to screw you over for doing this, but it can result in your code breaking in unpredictable ways on different compilers or different optimization levels.
To my knowledge, the only safe way to treat a
float
as auint32_t
in C++17 is to usechar*
(or similar) pointers to copy bytes from thefloat
value into a separateuint32_t
value, like howstd::memcpy
does. (Trying to do this via a union type is also undefined behavior, so you unfortunately are required to copy the bytes into a separate buffer object.)C++20 has
std::bit_cast
, which is designed exactly for this purpose, but Codewars unfortunately does not support C++20 yet. :(Loading more items...