shifting bits

From: fabio de francesco (fmdf_at_tiscali.it)
Date: 08/29/04


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;



Relevant Pages

  • Re: Interrupt doesnt make it to the 8259 on a ASUS P4PE mobo
    ... unsigned numbers by powers of 2, and SRA (Shift Right Arithmetic) to ... divide signed numbers by powers of 2. ... send the line "unsubscribe linux-kernel" in ...
    (Linux-Kernel)
  • Re: (OT) Do Not Call Registry Violator
    ... I must say that the funeral home we chose when my brother died ... a DNR, a Medical Directive and a Will, along with two Powers of Attorney - ... one giving my daughter control over medical decisions and one giving her ... have copies of all the documents except the Power of Attorney, ...
    (rec.pets.cats.anecdotes)
  • Re: (OT) Do Not Call Registry Violator
    ... I must say that the funeral home we chose when my brother died ... have a DNR, a Medical Directive and a Will, along with two Powers of ... Attorney - one giving my daughter control over medical decisions and one ... Attorney, and they know where all the originals are. ...
    (rec.pets.cats.anecdotes)
  • Re: Aragorn&Glorifindel vs. the Nine
    ... he certainly had powers he chose ... I'll let the snide comment slide, as I assume it is overflow from your ...
    (rec.arts.books.tolkien)
  • Re: How to solve (2x + 3y)^3?
    ... involved in computing the binomial coefficients ... to compute,(quite a few multiplications and divisions ... You're probably right about the powers of 2 and 3, but I guess anyone reasonably well-versed in arithmetic knows the first few by heart. ... practise with it, ...
    (sci.math)