MᏫᎻᎯᎷᎷᎬᎠ
Aghh that sucks
BinaryByter
BinaryByter
Aghh that sucks
yea, thats why shifts are a lt faster 🤔
MᏫᎻᎯᎷᎷᎬᎠ
Hmmm
Daniele°
how do you get a modulo by shifting? @ollirz @Drazzy ?
No shift, modulo use and for Power of two
BinaryByter
No, divisions are slower because they are done by loop
mov eax, DWORD PTR [rsp+12] .loc 1 8 1 add rsp, 24 .cfi_def_cfa_offset 8 .loc 1 7 16 mov edx, eax shr edx, 31 add eax, edx and eax, 1 sub eax, edx Oh
BinaryByter
Daniele°
wasnt aware of that 🤔
#define FAST_MOD(N,M) ((N)&((M)-1)) ... ... 10%2==FAST_MOD(10,2);
BinaryByter
🤔
Wim
we are talking about the modulo part of the division 😉
Shift it over the carry into a second register for example
BinaryByter
Nice :D
Wim
Or just copy and and it
BinaryByter
wait thats a lot better than what the compiler does
Wim
It misses stuff you're hiding
BinaryByter
huh?
Zorrito
I overloaded the >> operator, and it seems to call the overloaded version every time I use >>. Is there a way I can indicate when I want to use the overloaded version?
Zorrito
operator >> participates in overload resolution (as regular functions do)
I'm trying to do (stringstream) >> (size_t), but I found out it's using my overloaded operator (ap_uint) >> (ap_uint) where ap_uint is a class.
Zorrito
I'm trying to do (stringstream) >> (size_t), but I found out it's using my overloaded operator (ap_uint) >> (ap_uint) where ap_uint is a class.
I do have a constructor ap_uint(const std::string& s), but I don't have one for ap_uint(const std::size_t st).
Palinuro
it takes 3 years, including the time it takes for the insight that C++ is kinda shit
i'm migrating to golang and rust and i am the happiest dev on earth
Zorrito
Uh that could take a while lol. I'll get back to you.
binary
Please, how do I sort a struct that is an array with different data type members? For example: the struct is a product with an ID, NAME, PRICE. And I want to sort on the name. And please, one more thing, how do I print to the console like how more or less command works on Linux? i.e. part of the output at a time and print the rest as the user presses a key.
olli
Please, how do I sort a struct that is an array with different data type members? For example: the struct is a product with an ID, NAME, PRICE. And I want to sort on the name. And please, one more thing, how do I print to the console like how more or less command works on Linux? i.e. part of the output at a time and print the rest as the user presses a key.
I assume you mean "a array of structs". You can use either qsort and provide a comparison functions (in C) or use std::sort and a custom comparator (in C++). See: https://en.cppreference.com/w/c/algorithm/qsort https://en.cppreference.com/w/cpp/algorithm/sort
binary
I assume you mean "a array of structs". You can use either qsort and provide a comparison functions (in C) or use std::sort and a custom comparator (in C++). See: https://en.cppreference.com/w/c/algorithm/qsort https://en.cppreference.com/w/cpp/algorithm/sort
Thank you. I will check it out. Please, how about the output printing to the console like how LESS command works on Linux i.e. printing some of the output and pausing when the screen is full?
professor
Rand vs srand vs <random> for secure randomizes
olli
Rand vs srand vs <random> for secure randomizes
http://eel.is/c++draft/c.math.rand#3
Mihail
Why we just not prevent the error of "out of the index bound" by using the % arithmetic operator Like if we have an array of size 10 And the user asked for the 15th element We just write arr[15%10]
That makes 0 sense. Let's look for example at an off-by-one error. So you have an array of size 10, but some because of some bug it turned out that you tried reading 11th element. So if you don't do anything you'll either get some completely unrelated value or segfault. If you do your stupid modulo thing you'll be delivering the 1st element like nothing wrong happened. How's that going to help?
Mihail
Like your aim should be to discover such bugs not hide them from yourself
Mihail
It's like actually cleaning your room, instead of just shoving everything visible under the carpet
MᏫᎻᎯᎷᎷᎬᎠ
Like nothing happened
Mihail
Well you aren't preventing the bug
Mihail
You're hiding it from yourself
MᏫᎻᎯᎷᎷᎬᎠ
I didn't say we have to use this approach everytime
MᏫᎻᎯᎷᎷᎬᎠ
Okay If that so Why we didn't say the same thing on primitive types?! char c = 256: It will be truncated to 0
MᏫᎻᎯᎷᎷᎬᎠ
Like a program that is asking for input of the ascii code and then provide the corresponding char
MᏫᎻᎯᎷᎷᎬᎠ
Mihail
Like a program that is asking for input of the ascii code and then provide the corresponding char
You should be checking that and reasking for input/throwing an exception if it's wrong
MᏫᎻᎯᎷᎷᎬᎠ
It is
No It's implementation defined
Mihail
Like what do you want c to be?
MᏫᎻᎯᎷᎷᎬᎠ
Like what do you want c to be?
The same thing What do I want from an array that is 10 elements to be?!
Mihail
So it's not possible
Mihail
MᏫᎻᎯᎷᎷᎬᎠ
There's no way to store 256 in a char
Yeah But it will be truncated
Mihail
Like you want it to automatically find you a bigger type to store it?
Mihail
If yes maybe try JS
Mihail
No
Then what?
MᏫᎻᎯᎷᎷᎬᎠ
What happens in the primitive types must happen in the array
MᏫᎻᎯᎷᎷᎬᎠ
No
So this is the new point of discussion
MᏫᎻᎯᎷᎷᎬᎠ
Why?!
Mihail
Why?!
Because it's worse than just accessing the wrong element
olli
What happens in the primitive types must happen in the array
who says the pointer type doesn't wrap around?
Mihail
With primitive types it's just not possible of getting the better scenario
Mihail
What?!!
Pointers have a size
MᏫᎻᎯᎷᎷᎬᎠ
MᏫᎻᎯᎷᎷᎬᎠ
If someone afraid of hiding in the carpet He can also use exceptions, right?!
Mihail
Like ar[11] == ar[0]?
No but arr[std::numeric_limits<intptr_t>::max() + 1] would
olli
What?!!
int foo() { int ar[] = { 1 }; return ar[static_cast<uintptr_t >(-1) + 1]; } the size of the address does not depend on the array size but on the pointer size. And uintptr_t wraps around as well (as every unsigned integral type)
olli
If someone afraid of hiding in the carpet He can also use exceptions, right?!
yes, that's why std::vector<T>::at exists, it does a bounds check and throws exceptions
olli
What is unintptr_t?!
unsigned integer type capable of holding a pointer (the type is optional though, on some platforms pointer are bigger than integral types)
MᏫᎻᎯᎷᎷᎬᎠ
So Do you say that casting is more efficient than just using the module?!
olli
So Do you say that casting is more efficient than just using the module?!
no. I just all integral types behave the same in regard to wrapping
Anonymous
Hiii
MᏫᎻᎯᎷᎷᎬᎠ
I hope that wasn't another dumb question
MᏫᎻᎯᎷᎷᎬᎠ
Hiii
Hey