Recent

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

Key-Real

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

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

Fibonacci

  • Hero Member
  • *****
  • Posts: 613
  • 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;

Thaddy

  • Hero Member
  • *****
  • Posts: 16196
  • 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
  3.     addr: array[0..2] of byte;
  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
  4.     addr: array[0..2] of byte;
  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 »
If I smell bad code it usually is bad code and that includes my own code.

Fibonacci

  • Hero Member
  • *****
  • Posts: 613
  • 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
  3.     addr: array[0..2] of byte;
  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][+][-]
  1. tag.addr := 1234;

Thaddy

  • Hero Member
  • *****
  • Posts: 16196
  • 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 »
If I smell bad code it usually is bad code and that includes my own code.

Red_prig

  • Full Member
  • ***
  • Posts: 153
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
  3.     addr: 0..(1 shl 24)-1;
  4.     len: Byte;      
  5.     r0, g0, b0, code: Byte;
  6.   end;

Thaddy

  • Hero Member
  • *****
  • Posts: 16196
  • Censorship about opinions does not belong here.
Re: how to translate typedef with bits
« Reply #6 on: June 21, 2024, 08:37:00 pm »
Same complaint.
If I smell bad code it usually is bad code and that includes my own code.

 

TinyPortal © 2005-2018