answersLogoWhite

0

Can you perform bitwise circular shift in c plus plus?

Updated: 8/18/2019
User Avatar

Wiki User

11y ago

Best Answer

Yes. The following template examples will work for all unsigned integers of length 1, 2, 4 and 8 bytes.

#include <type_traits> // for std::is_unsigned

template<class T>

T circular_shift_left( register T x )

{

static_assert( std::is_unsigned<T>::value, "circular_shift_left(T): T must be unsigned!\n" );

T bit = x;

switch( sizeof( x ))

{

case(1): bit &= (T) 0x80; break;

case(2): bit &= (T) 0x8000; break;

case(4): bit &= (T) 0x80000000; break;

case(8): bit &= (T) 0x8000000000000000; break;

default: assert( 0 );

}

x <<= 1;

x |= bit ? 1 : 0;

return( x );

}

template<class T>

T circular_shift_right( register T x )

{

static_assert( std::is_unsigned<T>::value, "circular_shift_right(T): T must be unsigned!\n" );

T bit = x & (T) 0x1;

switch( sizeof( x ))

{

case(1): bit = bit ? (T) 0x80 : 0x0; break;

case(2): bit = bit ? (T) 0x8000 : 0x0; break;

case(4): bit = bit ? (T) 0x80000000 : 0x0; break;

case(8): bit = bit ? (T) 0x8000000000000000 : 0x0; break;

default: assert( 0 );

}

x >>= 1;

x |= bit;

return( x );

}

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can you perform bitwise circular shift in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the use of bitwise operator in c plus plus?

They perform bitwise operations like AND (&amp;), OR (|), XOR (^) and NOT (~).


What are logical classes in c plus plus?

There is no such thing. Logic is bitwise operation, not a data type.


How do you swap two numbers with bitwise operator in c plus plus?

// Note: ^ is the XOR operator a = a ^ b b = b ^ a a = a ^ b


How is a bitwise copy created in c plus plus?

You could just use memcpy(3), using sizeof() to get the object size.


How do you undo the ctrl plus shift plus equals?

ctrl shift - reverses the effect of ctrl shift =


What are the conditions to swap in c plus plus without temporary?

You can swap two integers without temporary storage by bitwise exclusive-or'ing them in a specific sequence...a ^= b;b ^= a;a ^= b;


What are the various operators available in c language?

There are eight types of operators which are used in C language.These are- 1.Arithmetic operator 2.Assignment operator 3.Relational operator 4.Increment/Decrement operator 5.Bitwise operator 6.Logical operator 7.Conditional operator 8.Additional operator 1.Arithmetic operator:Arithmetic operators are mathmetical operator.These are addition,Subtraction,Multiplication and divison. 2.Assignment operator:Assignment operators are used to store the result of an expression to a variable.


What does ctrl plus shift plus plus sign do?

nothing


What is operator associativity in C?

OperatorDescriptionAssociativity()[].->++ -- Parentheses (function call) (see Note 1)Brackets (array subscript)Member selection via object nameMember selection via pointerPostfix increment/decrement (see Note 2) left-to-right++ --+ -! ~(type)*&sizeof Prefix increment/decrementUnary plus/minusLogical negation/bitwise complementCast (change type)DereferenceAddressDetermine size in bytes right-to-left * / % Multiplication/division/modulus left-to-right + - Addition/subtraction left-to-right > Bitwise shift left, Bitwise shift right left-to-right < >= Relational less than/less than or equal toRelational greater than/greater than or equal to left-to-right == != Relational is equal to/is not equal to left-to-right & Bitwise AND left-to-right ^ Bitwise exclusive OR left-to-right | Bitwise inclusive OR left-to-right && Logical AND left-to-right Logical OR left-to-right ?: Ternary conditional right-to-left =+= -=*= /=%= &=^= |== AssignmentAddition/subtraction assignmentMultiplication/division assignmentModulus/bitwise AND assignmentBitwise exclusive/inclusive OR assignmentBitwise shift left/right assignment right-to-left ,Comma (separate expressions) left-to-right


What is ctrl plus shift plus plus sign?

It zooms in on the page.


How is a bitwise copy made in c plus plus?

A bitwise copy is what you automatically get when you do not provide a copy constructor. The compiler simply provides code that copies the object without regard to the type of the members. This is dangerous if any of the members happen to be pointers, because then you have two pointers to the same object and, if you delete one, you wind up having the other pointing to an object that has been deleted.


What is the C plus plus expression and operator precedence?

Precedence is determined by operators only. Every operator has a precedence in the range 1 through 17, where 1 has the highest precedence. All precedences have left-to-right associativity except 3 and 15 which are right-to-left. Precedence 1: scope-resolution operator Precedence 2: postfix increment/decrement, function-style type cast, function call, array subscripting and selection by reference or pointer. Precedence 3: prefix increment/decrement, unary plus/minus, logical and bitwise NOT, C-style cast, dereferencing, address-of, sizeof, new/new [] and delete/delete []. Precedence 4: pointer to member. Precedence 5: multiplication, division and modulo. Precedence 6: addition and substraction. Precedence 7: bitwise left/right shift. Precedence 8: relational operators (&lt;, &lt;=, &gt; and &gt;=). Precedence 9: equal/not equal operators (= and !=) Precedence 10: bitwise AND Precedence 11: bitwise XOR Precedence 12: bitwise OR Precedence 13: logical AND Precedence 14: llogical OR Precedence 15: ternary conditional, assignment and compound assignment. Precedence 16: throw Precedence 17: comma