C++ equilivent of "triple shift" in Java

Started by
7 comments, last by GameDev.net 18 years, 10 months ago
op1 >>> op2 - I read on Sun's website that that operator shifts bits of op1 right by distance op2; fills with zero bits on the left-hand side, but I'm not sure how to do that.
//------------------------------------------------------------------------------------------------------The great logician Bertrand Russell once claimed that he could prove anything if given that 1+1=1. So one day, some fool asked him, "Ok. Prove that you're the Pope." He thought for a while and proclaimed, "I am one. The Pope is one. Therefore, the Pope and I are one."
Advertisement
In C/C++, signed integers are sign extended when shifting right, while unsigned integers are 0 extended. Practically speaking, its the same operation because unsigned integers are always positive and so they would get 0s even if they were signed. As I recall, Java doesn't have unsigned types, hence the need for a separate operator to provide the same functionality.
int i;unsigned int j;i >> 1; //equivilent to Java >>, 1s are padded if i is negative, 0s if i is positivej >> 1; //equivilent to Java >>>, 0s are always added.

So if you need that behavior, either use unsigned ints, or cast to an unsigned ints before shifting.
int i = -1; //0xFFint j = i >> 1; //0xFFint k = (unsigned)i >> 1; //0x7F

CM
Quote:Original post by Conner McCloud
In C/C++, signed integers are sign extended when shifting right, while unsigned integers are 0 extended.

Careful there... whether integers use arithmetic shift or logical shift is implementation-defined. After all, the standard doesn't even require two's-complement arithmetic.
Quote:Original post by Sneftel
Quote:Original post by Conner McCloud
In C/C++, signed integers are sign extended when shifting right, while unsigned integers are 0 extended.

Careful there... whether integers use arithmetic shift or logical shift is implementation-defined. After all, the standard doesn't even require two's-complement arithmetic.

Oh, so it is. I stand corrected. If I'm reading this correctly, though, it is only for negative values that it is implementation defined...unsigned values are guarenteed to be logical shifts, yes? So using unsigned ints in the first place is still a valid solution.

CM
Actually, the standard is extremely close-mouthed on the semantics of bitshifting... an implementation could choose to define "right-shift" as "multiply by seven" and be considered technically compliant. But yeah, if it is not standard-defined, it is at least sanity-defined. [wink]
Quote:Original post by Sneftel
Actually, the standard is extremely close-mouthed on the semantics of bitshifting... an implementation could choose to define "right-shift" as "multiply by seven" and be considered technically compliant. But yeah, if it is not standard-defined, it is at least sanity-defined. [wink]

Oh...I was under the impression the >> and << operators were defined in terms of multiplication/division by 2^n rather than bit shifting. That way, even if the underlying process changes from one platfrom to the next you knew the result won't.

Final thought, is integer division sufficiently well defined that one could perform the division by 2^n yourself if that's the behavior you want? If the implementation is allowed to chose between rounding up and rounding down, then I'm guessing the answer is no.

CM
I'm reasonably sure that integer division is required to round toward 0. But I don't have the standard in front of me and I've been wrong before.

Quote:I was under the impression the >> and << operators were defined in terms of multiplication/division by 2^n rather than bit shifting
I don't mean to dig on you but I find that hilarious :). Using bit shifts to divide by powers of two is a very low-level optimization that has absolutely zero practical effect in the vast majority of cases, yet it has become so enshrined in the C/C++ culture that it's now coming full circle and changing the very definition of the operators [totally].
-Mike
Quote:Original post by Anon Mike
I don't mean to dig on you but I find that hilarious :). Using bit shifts to divide by powers of two is a very low-level optimization that has absolutely zero practical effect in the vast majority of cases, yet it has become so enshrined in the C/C++ culture that it's now coming full circle and changing the very definition of the operators [totally].

Presumably, the standards committee was interested in defining things in such a way that as few ambiguities as possible exist in the basic operators. I assume, for instance, that addition is defined in such a way that 4 + (-2) always yields 2, whatever numeric representation the system uses. Applying this same logic to >> and << requires that a bit-independant definition be provided. Enter 4 >> 2 == 4 / (2^2). When you consider that "shift right" and "shift left" have no meaning even in a binary system when you consider computer representation of numbers, the usefulness of such a definition becomes clear.

CM
Quote:When you consider that "shift right" and "shift left" have no meaning even in a binary system when you consider computer representation of numbers, the usefulness of such a definition becomes clear.


what on earth does that mean?

This topic is closed to new replies.

Advertisement