?
Solved

How to write a bit in a record?

Posted on 2001-07-12
11
Medium Priority
?
166 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 9

Expert Comment

by:ITugay
ID: 6276985
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
ID: 6277016
Take a look at TBits...
0
 

Author Comment

by:comptebidon81
ID: 6277020
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:comptebidon81
ID: 6277028
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
ID: 6277074
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6277101
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
ID: 6277217
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
ID: 6277359
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 300 total points
ID: 6277450
guessing not possible
the smallest part of a record is a byte
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6278410
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
ID: 6285612
Well by telling me it was not possible, you answered my question!
Your welcome.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month9 days, 20 hours left to enroll

762 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