Solved

How to write a bit in a record?

Posted on 2001-07-12
11
158 Views
Last Modified: 2010-04-06
I am building a record of Bytes and I have to split one of those Bytes. (4bits-4bits or 1bit-7bits).
Anyone knows how to do so in a record?
0
Comment
Question by:comptebidon81
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 9

Expert Comment

by:ITugay
Comment Utility
Hi comptebidon81,

do you mean something like this?

var
  B: Byte;
  B1: Byte;
begin
  B := $FF;        // B  := 11111111
  B1 := B and $F0; // B1 := 11110000  get first for bits
  B1 := B1 shr 4;  // B1 := 00001111 shif it

end;
0
 
LVL 13

Expert Comment

by:Epsylon
Comment Utility
Take a look at TBits...
0
 

Author Comment

by:comptebidon81
Comment Utility
Actually, I'm looking for a way to store it in a record, so it would be easier to understand the structure of the file. Right now, I'm working with arrays of Bytes, and it is hard to understand the true meaning of the bytes, because they can be divided in up to 8 parts. It is also very confusing when I write down the file. (I have to make a lot of calculation for each byte).
0
 

Author Comment

by:comptebidon81
Comment Utility
TBits is like a list, isn't it?
How am I suppose to store a list of one item in a record?
0
 
LVL 9

Expert Comment

by:ITugay
Comment Utility
Ohh, I'v got it :-)
Seems the way is to create an object:

type
  TMyHeader = class(TObject)
  private
    FData: array[0..1] of byte;
    function GetFirst4Bits: Byte;
    procedure SetFirst4Bits(B: Byte);
  public
    procedure WriteToFile(F: File);
    procedure ReadFromFile(F: File);
    property First4bits: byte read GetFirst4bits write SetFirst4Bits;
  end;

......

function TMyHeader.GetFirst4Bits: Byte;
begin
  Result := (FData[0] and $F0) shr 4;
end;

procedure TMyHeader.SetFirst4Bits(B: Byte);
begin
  FData[0] := (FData[0] and $0F) or (B shl 4);
end;

------
Igor.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
maybe a set could help like

type TBits=(bit0,bit1,bit2,bit3,bit4,bit5,bit6,bit7);
Type TByte=set of TBits;

0
 
LVL 13

Expert Comment

by:Epsylon
Comment Utility
You can use these functions to set/clear/read bit in a byte:

procedure SetBit(var byte: Byte; bit: Byte);
begin
  byte := byte or (1 shl bit);
end;

procedure ClearBit(var byte: Byte; bit: Byte);
begin
  byte := byte and not (1 shl bit);
end;

function GetBit(byte, bit: Byte): Boolean;
begin
  Result := (byte and (1 shl bit)) <> 0;
end;
0
 

Author Comment

by:comptebidon81
Comment Utility
Sorry about the misunderstanding, but my problem is not about reading or setting the Bits. I need a way to include it in a record. Something that would look like this:
MyRecord = record
    1Bit : Bit;
    2Bits : array [1..2] of Bit;
end;
ya see?
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 100 total points
Comment Utility
guessing not possible
the smallest part of a record is a byte
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
hu,

not guessed this, even it was not a help,
are you sure you've pressed the correct button?

thanks for the points :-)

meikl ;-)
0
 

Author Comment

by:comptebidon81
Comment Utility
Well by telling me it was not possible, you answered my question!
Your welcome.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now