Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Delphi: bytes/bits

Posted on 2003-02-22
2
Medium Priority
?
402 Views
Last Modified: 2010-04-04
Hello, from my knowledge the lowest variable type in delphi is a byte (8 bits), but I am filling out a record to be used with the winsock api and two of the fields it requires has to be 4 bits long.

I am wondering how i could achieve this in delphi, in C it is quite easily done like this:

unsigned char firstfield:4, secondfield:4;

Basically, need a way to work with nibbles (4 bits) in delphi.

Antrix
0
Comment
Question by:Antrix
2 Comments
 
LVL 22

Accepted Solution

by:
grg99 earned 200 total points
ID: 7998982
The easiest way is to use shifts and masks to extract or combine the fields.

Example:  pack a and b into 4-bit fields into byte c:


c := ( a shl 4 ) or b;

( This assumes a and b are already 4 bits or less,
if not, and them with $000F:


c := ( (a and $000F) shl 4 ) or (b and $000F);

---------------------

Example, extract 4-bit fields a and b from c:

a := (c shr 4) and $000F;
b := (c      ) and $000F;

---------
That should handle most of your 4-bit needs.

0
 

Author Comment

by:Antrix
ID: 7999130
Thank you for your quick and accurate reply
0

Featured Post

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!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses

580 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