shifting bits
From: fabio de francesco (fmdf_at_tiscali.it)
Date: 08/29/04
- Next message: Jim Rogers: "Re: shifting bits"
- Previous message: Brian May: "Re: timeouts"
- Next in thread: Jim Rogers: "Re: shifting bits"
- Reply: Jim Rogers: "Re: shifting bits"
- Reply: Ludovic Brenta: "Re: shifting bits"
- Reply: Stephen Leake: "Re: shifting bits"
- Reply: tmoran_at_acm.org: "Re: shifting bits"
- Reply: Ole-Hjalmar Kristensen: "Re: shifting bits"
- Reply: Martin Krischik: "Re: shifting bits"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 28 Aug 2004 18:38:42 -0700
Hello.
I had to pack/unpack some variables into/from one. In order to get the
result I chose to shift the variables' bits using multiplications and
divisions by powers of two.
1) Does Ada have a built-in shift operand like C++ "<<" and ">>" ?
2) Can only variables of modular type be shifted ? If yes, why ?
3) Will the following code produce some "shl" and "shr" machine
instruction ?
date_y := date_y * 2**9; -- shift left
year := integer((date / 2**9) and offset_year); --shift right and
"and" offset
I thank you all in advance.
Ciao,
Fabio De Francesco.
P.S. The following is the complete code in the case someone wanted to
read it in the whole. I/O routines and values checks have been cut to
the minimum or removed.
with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO;
procedure packed_date is
type date_type is mod 2**20;
date, date_y, date_m, date_d : date_type := 0;
offset_year : constant date_type := 2**11-1;
offset_month : constant date_type := 2**4-1;
offset_day : constant date_type := 2**5-1;
year : integer range 1900 .. 2099 := 1900;
month : integer range 1 .. 12 := 1;
day : integer range 1 .. 31 := 1;
begin
get (year);
date_y := date_type(year);
date_y := date_y * 2**9;
get (month);
date_m := date_type(month);
date_m := date_m * 2**5;
get (day);
date_d := date_type(day);
date := date_y or date_m or date_d;
day := integer(date and offset_day);
put(day);
month := integer( (date / 2**5) and offset_month );
put(month);
year := integer( (date / 2**9) and offset_year );
put(year);
end packed_date;
- Next message: Jim Rogers: "Re: shifting bits"
- Previous message: Brian May: "Re: timeouts"
- Next in thread: Jim Rogers: "Re: shifting bits"
- Reply: Jim Rogers: "Re: shifting bits"
- Reply: Ludovic Brenta: "Re: shifting bits"
- Reply: Stephen Leake: "Re: shifting bits"
- Reply: tmoran_at_acm.org: "Re: shifting bits"
- Reply: Ole-Hjalmar Kristensen: "Re: shifting bits"
- Reply: Martin Krischik: "Re: shifting bits"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|