C : Array of bytes which stores unsigned ints using 3 bytes - c

this is my first question so please take me easy, I'm a beginner in C and I'd like to know more about it.
Could someone help me write this function? I can't really picture how the array of bytes should look like:
"An array of bytes stores unsigned ints each using 3 bytes, least significant byte first. Write a function that takes an array and its byte length and prints the numbers in hex."

[lowbyte-of-value-0, middlebyte-of-value-0, highbyte-of-value-0,lowbyte-of-value-1, middlebyte-of-value-1, highbyte-of-value-1, ... lowbyte-of-value-n, middlebyte-of-value-n, highbyte-of-value-n]
Your task is to walk through that array, pull the bytes one by one or in groups of 3 as you choose, and reassemble them into unsigned integer values.

Related

what size do we consider the pointer of 2d array in space complexity?

This is the code given in my algorithms book.We need to calculate its space complexity.
this is the answer given-
The space complexity is S=C+Sp and in this case Sp is zero as the code is independent of n.
But I wanted to calculate the C part of the code and in this case it would be 5*2bytes=10 bytes considering a,x,n,0 and -1.
So my question is what would be the C in case a was a 2d matrix,do we consider it as 2 bytes only as in the case of 1d array or we take it as 4 bytes?
Pointers and integers are no more 16 bits since the mid-eighties so I wonder what you mean with your 2 bytes.
Also note that your accounting of space is a little weird. You don't count the variable i, but you do count literal constants, which usually appear as immediate arguments in the assembly code.
Anyway, on a 32 bits machine, all addresses (and pointers) are represented on 4 bytes and the extra accounting of array dimensions is performed by the compiler and hard-coded into the assembly.
On a 64 bits machine, count 8 bytes per pointer and 4 bytes per int.

System Verilog - Reading a line from testbench and splitting the data

I'm a beginner in SystemVerilog Programming. I have a file called "input.in" and it has around 32 bits of data. The value is present in only one line of the file.
The data once sent from the testbench must be split into an array or 4 variables, each containing only 8 bits of the input. Please. Somebody help me :(
I think, you want to split the 32 bits of data into 4 bytes of data.
Please try the following:
{>>{a,b,c,d}} = var_32_bit ; //a,b,c,d are 8 bits variable.
// var_32_bit is an array of 32 bits size or a 32-bit variable. {bit a[] or bit [31:0]}
Is this the one you need ?

Why does an int take up 4 bytes in c or any other language? [duplicate]

This question already has answers here:
size of int variable
(6 answers)
Closed 5 years ago.
This is a bit of a general question and not completely related to the c programming language but it's what I'm on studying at the moment.
Why does an integer take up 4 bytes or How ever many bytes dependant on the system?
Why does it not take up 1 byte per integer?
For example why does the following take up 8 bytes:
int a = 1;
int b = 1;
Thanks
I am not sure whether you are asking why int objects have fixed sizes instead of variable sizes or whether you are asking why int objects have the fixed sizes they do. This answers the former.
We do not want the basic types to have variable lengths. That makes it very complicated to work with them.
We want them to have fixed lengths, because then it is much easier to generate instructions to operate on them. Also, the operations will be faster.
If the size of an int were variable, consider what happens when you do:
b = 3;
b += 100000;
scanf("%d", &b);
When b is first assigned, only one byte is needed. Then, when the addition is performed, the compiler needs more space. But b might have neighbors in memory, so the compiler cannot just grow it in place. It has to release the old memory and allocate new memory somewhere.
Then, when we do the scanf, the compiler does not know how much data is coming. scanf will have to do some very complicated work to grow b over and over again as it reads more digits. And, when it is done, how does it let you know where the new b is? The compiler has to have some mechanism to update the location for b. This is hard and complicated and will cause additional problems.
In contrast, if b has a fixed size of four bytes, this is easy. For the assignment, write 3 to b. For the addition, add 100000 to the value in b and write the result to b. For the scanf, pass the address of b to scanf and let it write the new value to b. This is easy.
The basic integral type int is guaranteed to have at least 16 bits; At least means that compilers/architectures may also provide more bits, and on 32/64 bit systems int will most likely comprise 32 bits or 64 bits (i.e. 4 bytes or 8 bytes), respectively (cf, for example, cppreference.com):
Integer types
... int (also accessible as signed int): This is the most optimal
integer type for the platform, and is guaranteed to be at least 16
bits. Most current systems use 32 bits (see Data models below).
If you want an integral type with exactly 8 bits, use the int8_t or uint8_t.
It doesn't. It's implementation-defined. A signed int in gcc on an Atmel 8-bit microcontroller, for example, is a 16-bit integer. An unsigned int is also 16-bits, but from 0-65535 since it's unsigned.
The fact that an int uses a fixed number of bytes (such as 4) is a compiler/CPU efficiency and limitation, designed to make common integer operations fast and efficient.
There are types (such as BigInteger in Java) that take a variable amount of space. These types would have 2 fields, the first being the number of words being used to represent the integer, and the second being the array of words. You could define your own VarInt type, something like:
struct VarInt {
char length;
char bytes[]; // Variable length
}
VarInt one = {1, {1}}; // 2 bytes
VarInt v257 = {2, {1,1}}; // 3 bytes
VarInt v65537 = {4, {1,0,0,1}}; // 5 bytes
and so on, but this would not be very fast to perform arithmetic on. You'd have to decide how you would want to treat overflow; resizing the storage would require dynamic memory allocation.

I am having 24 individual bits (1 or 0), want to form a byte array of size 3 (forming 3bytes) with these bits and require a pointer for it

24bits corresponds to different parameters, with each parameter taking varying number of bits (taking up value either 1 or 0).
Need to create a byte array (or unsigned char array) so that all these 24bits will fit in and there will be no memory loss (which will happen in case of taking int data type)
Require a byte pointer so that upon incrementing the pointer it would seek the location 0,8,16 in the byte array formed.
I couldnt get a proper method to do it as I couldnt able to access in case of byte array (say from 0 to 23 bits). Kindly help me with this in C program
**Example:**
>**Available-**
>a=0; b=1; c=1; d=0;... till 23 parameters
>**Required-**
>byte arr[3] containing above 23 parameters
>byte *p
>p=arr to access every 8 bit
You can use bits field.
Where there is a guide
Bits field

Write 9 bits binary data in C

I am trying to write to a file binary data that does not fit in 8 bits. From what I understand you can write binary data of any length if you can group it in a predefined length of 8, 16, 32,64.
Is there a way to write just 9 bits to a file? Or two values of 9 bits?
I have one value in the range -+32768 and 3 values in the range +-256. What would be the way to save most space?
Thank you
No, I don't think there's any way using C's file I/O API:s to express storing less than 1 char of data, which will typically be 8 bits.
If you're on a 9-bit system, where CHAR_BIT really is 9, then it will be trivial.
If what you're really asking is "how can I store a number that has a limited range using the precise number of bits needed", inside a possibly larger file, then that's of course very possible.
This is often called bitstreaming and is a good way to optimize the space used for some information. Encoding/decoding bitstream formats requires you to keep track of how many bits you have "consumed" of the current input/output byte in the actual file. It's a bit complicated but not very hard.
Basically, you'll need:
A byte stream s, i.e. something you can put bytes into, such as a FILE *.
A bit index i, i.e. an unsigned value that keeps track of how many bits you've emitted.
A current byte x, into which bits can be put, each time incrementing i. When i reaches CHAR_BIT, write it to s and reset i to zero.
You cannot store values in the range –256 to +256 in nine bits either. That is 513 values, and nine bits can only distinguish 512 values.
If your actual ranges are –32768 to +32767 and –256 to +255, then you can use bit-fields to pack them into a single structure:
struct MyStruct
{
int a : 16;
int b : 9;
int c : 9;
int d : 9;
};
Objects such as this will still be rounded up to a whole number of bytes, so the above will have six bytes on typical systems, since it uses 43 bits total, and the next whole number of eight-bit bytes has 48 bits.
You can either accept this padding of 43 bits to 48 or use more complicated code to concatenate bits further before writing to a file. This requires additional code to assemble bits into sequences of bytes. It is rarely worth the effort, since storage space is currently cheap.
You can apply the principle of base64 (just enlarging your base, not making it smaller).
Every value will be written to two bytes and and combined with the last/next byte by shift and or operations.
I hope this very abstract description helps you.

Resources