# C++ equilivent of "triple shift" in Java

This topic is 4743 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
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

##### Share on other sites
Quote:
 Original post by Conner McCloudIn 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.

##### Share on other sites
Quote:
Original post by Sneftel
Quote:
 Original post by Conner McCloudIn 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

##### Share on other sites
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]

##### Share on other sites
Quote:
 Original post by SneftelActually, 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

##### Share on other sites
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].

##### Share on other sites
Quote:
 Original post by Anon MikeI 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

##### Share on other sites
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?

• 37
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631360
• Total Posts
2999556
×