?
Solved

Delphi: bytes/bits

Posted on 2003-02-22
2
Medium Priority
?
379 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
[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
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

765 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