TURBO PASCAL

Новости

Программы   

Turbo Pascal 

Игры

Документация   

Странности

FAQ

Ссылки

Форум

Гостевая книга

Рассылка

Благодарности

Об авторе

35: I have a variable of type BYTE and would like to extract two
numbers from it. (The first 4 bits making up number A, the second 4bits making up number B).  How can I extract these two numbers?

 A: Ah, this questions bring back the good bad old days of the
Commodore C64 programming when bit operations were rather a rule
than an exception. Here is the solution.
  function HIBYTEFN (x : byte) : byte;
  begin
    hibytefn := x Shr 4;           (* Shift right by four bits *)
  end;
  {}
  function LOBYTEFN (x : byte) : byte;
  begin
    lobytefn := x and 15;          (* x and 00001111 *)
  end;
From Patrick Taylor (exuptr@exu.ericsson.se): Ah, leave it to Timo
to come up with a different way! An other is (n div 16)
(n mod 16).
   Patrick is right.  But unless the compiler is optimized, the
former produces more efficient code. Not that it really makes any
practical difference whatsoever.
   Of course the fastest code is produced using assembler as pointed
out by Maarten Pennings (maarten@cs.ruu.nl) who provided the
following inline example:
  function high(b:byte):byte;
    inline($58         { POP AX      | AH=?, AL=b       }
          /$30/$e4     { XOR AH,AH   | AH=0, AL=b       }
          /$b9/$04/$00 { MOV CX,0004 | AH=0, AL=b, CL=4 }
          /$d3/$e8     { SHR AX,CL   | AX=b shr 4       }
          );
 A2: Getting a word from a longint can alternatively be achieved
without any calculations by using a kind of typecasting. Below is
the code I have utilized in ftp://garbo.uwasa.fi/pc/ts/tspa3470.zip.
  (* Get the high-order word of the longint argument *)
  function HIWORDFN (x : longint) : word;
  type type1 = record
                 low  : word;
                 high : word;
               end;
  var m1 : type1 absolute x;
  begin
    hiwordfn := m1.high;
  end;  (* hiwordfn *)
 

На первую страницу

Rambler's Top100 Rambler's Top100
PROext: Top 1000

(с)Все права защищены

По всем интересующим вопросам прошу писать на электронный адрес

Hosted by uCoz