### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: how to translate typedef with bits  (Read 746 times)

#### Key-Real

• Sr. Member
• Posts: 361
##### how to translate typedef with bits
« on: June 21, 2024, 02:26:31 pm »
how to translate this?

Code: Pascal  [Select][+][-]
1. typedef struct {
3.         unsigned        len:   8;
4.         u_char          r0, g0, b0, code;
5. } P_TAG;
6.

#### Fibonacci

• Sr. Member
• Posts: 453
• Internal Error Hunter
##### Re: how to translate typedef with bits
« Reply #1 on: June 21, 2024, 02:42:28 pm »
Just use GPT

Code: Pascal  [Select][+][-]
1. type
2.   P_TAG = bitpacked record
3.     addr: 0..16777215; // 24-bit address (24 bits can represent numbers from 0 to 16777215)
4.     len: 0..255;       // 8-bit length (8 bits can represent numbers from 0 to 255)
5.     r0, g0, b0, code: Byte;
6.   end;

• Hero Member
• Posts: 15324
• Censorship about opinions does not belong here.
##### Re: how to translate typedef with bits
« Reply #2 on: June 21, 2024, 08:18:07 pm »
Code: Pascal  [Select][+][-]
1. type
2.   P_TAG = bitpacked record
4.     len: byte;
5.     r0, g0, b0, code: Byte;
6.   end;
makes more sense to me than GPT.

btw, the P_ indicates a pointer type, in that case:
Code: Pascal  [Select][+][-]
1. type
2.   P_TAG = ^T_TAG;
3.   T_TAG = bitpacked record
5.     len: byte;
6.     r0, g0, b0, code: Byte;
7.   end;
This also explains the len field.
It doesn't need to be bitpacked, just packed.
« Last Edit: June 21, 2024, 08:25:26 pm by Thaddy »
My great hero has found the key to the highway. Rest in peace John Mayall.
Playing: "Broken Wings" in your honour. As well as taking out some mouth organs.

#### Fibonacci

• Sr. Member
• Posts: 453
• Internal Error Hunter
##### Re: how to translate typedef with bits
« Reply #3 on: June 21, 2024, 08:22:53 pm »
Code: Pascal  [Select][+][-]
1. type
2.   P_TAG = bitpacked record
4.     len: byte;
5.     r0, g0, b0, code: Byte;
6.   end;
makes more sense to me than GPT.

Depends on the use case, you cant do this in your version:

Code: Pascal  [Select][+][-]

• Hero Member
• Posts: 15324
• Censorship about opinions does not belong here.
##### Re: how to translate typedef with bits
« Reply #4 on: June 21, 2024, 08:31:11 pm »
That would be a simple cast. I think my approach is closer to what is actually meant.
Proper code would probably be a variant record, but that is not how the C code translates.
I am aware of that.
« Last Edit: June 21, 2024, 08:46:42 pm by Thaddy »
My great hero has found the key to the highway. Rest in peace John Mayall.
Playing: "Broken Wings" in your honour. As well as taking out some mouth organs.

#### Red_prig

• Full Member
• Posts: 152
##### Re: how to translate typedef with bits
« Reply #5 on: June 21, 2024, 08:32:45 pm »
Code: Pascal  [Select][+][-]
1. type
2.   P_TAG = bitpacked record
4.     len: Byte;
5.     r0, g0, b0, code: Byte;
6.   end;