Click here to Skip to main content
15,887,313 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So i have these functions, which works perfectly:
C++
void pushw(int16_t x) {
	push((x & 0xFF00) >> 8);
	push(x & 0x00FF);
}

void pushd(int32_t x) {
	pushw((x & 0xFFFF0000) >> 16);
	pushw(x & 0x0000FFFF);
}


But when i do this:
C++
void pushq(int64_t x) {
	pushq((x & 0xFFFFFFFF00000000) >> 32);
	pushq(x &  0x00000000FFFFFFFF);
}


It compiles but it skips all the prints i have in the main function, from where i am also calling the push functions. Why is this?

What I have tried:

I have not tried anything, i am just utterly confused why it won't listen to my input.
Posted
Updated 12-Mar-23 12:55pm
Comments
k5054 25-Feb-23 15:26pm    
should pushq call pushd, or is that a typo?
Jamie Engel 26-Feb-23 12:39pm    
yes it was a typo, it wasn't meant to be infinitely recursive haha. I don't know how to delete my question though, seems kind of unnecessary to take up bandwidth just because i was tired and forgot to check the spelling xD

Look at your code:
void pushq(int64_t x) {
	pushq((x & 0xFFFFFFFF00000000) >> 32);
	pushq(x &  0x00000000FFFFFFFF);
}
So pushq calls pushq, which calls pushq, which ... continues until out of stack space and your app crashes.
Did you mean to call pushd instead? If so, you probably want to cast the values as well to avoid warnings on some compilers.
 
Share this answer
 
v2
The constant 0xFFFFFFFF00000000 is truncated to 32 bits, yielding 0. You should append "ll" to indicate that the constant is 64 bits wide: 0xFFFFFFFF00000000ll. Note that this is two times lower case L, and not number eleven or the logical OR operator.
An alternative solution is to just call pushd(x>>32).
 
Share this answer
 
Quote:
Bitmasks not working on 64-bit values.

C++ compiler use a default datatype if 32 bits if it is not told otherwise. There is no type inference, you are responcible of telling the compiler which datatype to use.
Below links speak mainly about cast on variables, but it also apply to constants in formulas:
C++ Casting Operators[^]
Const cast in C[^]
Explicit type conversion - cppreference.com[^]
So:
C++
void pushq(int64_t x) {
	pushq((x & 0xFFFFFFFF00000000) >> 32);
	pushq(x &  0x00000000FFFFFFFF);
}

and
C++
void pushq(int64_t x) {
	pushq((x & (ll)0xFFFFFFFF00000000) >> 32);
	pushq(x &  (ll)0x00000000FFFFFFFF);
}

gives different results.
 
Share this answer
 
There was a misspelling. It was meant to call the function pushd, not itself (pushq).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900