Bit shifts: Difference between revisions

Ada
(php)
(Ada)
Line 8:
 
If possible, perform the right shifts on a negative number, so one can distinguish between the arithmetic and logical shifts.
 
=={{header|Ada}}==
Ada shifting operations only work on unsigned numbers. The right shift arithmetic shifts one bits if the value being shifted is at least half the modulus of the unsigned type. The example below uses an 8-bit unsigned type and a value of 128 to demonstrate this behavior. Ada also provide functions to rotate left and rotate right, which are not part of this task.
<ada>
with Interfaces; use Interfaces;
with Ada.Text_Io; use Ada.Text_Io;
 
procedure Bit_Shifts is
X : Unsigned_8 := 128;
N : Natural := 1;
begin
Put_Line(Unsigned_8'Image(Shift_Left(X, N))); -- Left shift
Put_Line(Unsigned_8'Image(Shift_Right(X, N))); -- Right shift
Put_Line(Unsigned_8'Image(Shift_Right_Arithmetic(X, N))); -- Right Shift Arithmetic
end Bit_Shifts;
</ada>
Output of the above program:
0
64
192
 
 
=={{header|C}}==
Anonymous user