(*(pucBuf)++) = (unsigned char) (usValue >> 8); [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I got this C language code from the client,which is about message encapsulation,but I don't have a good understanding of this code.
Can someone please explain what it's doing?

(*(pucBuf)++) = (unsigned char) (usValue >> 8);
can be read as:
get the value of usValue and right-shift it 8 bits;
cast that to an unsigned char type;
store it into the memory location pointed to by the pubBuf pointer;
advance pucBuf to point to the next sequential item of its type.
Most likely it's taking the high-order eight bits of a sixteen(-or-more)-bit value and storing that into a memory buffer, within some sort of loop.

Related

C program: multiply two 32 bit value & return in 64-bit on a processor having a 16*16 multiplier [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
In a recent interview, I was asked the following coding problem. Could someone please help me understand how to address this problem in terms of C coding?
Q.
A processor has a 16*16 multiplier to represent 32-bit value.
Take two 32 bit value, multiply them and return in 64 bit format.
Let's call the 32 bit numbers A and B. Now, let's split them into 16 bit numbers, a0/b0 and a1/b1 (a0 is the bigger part).
Now, A*B == (a0<<16+a1) * (b0<<16+b1) == (a0 * b0) << 32 + (a1 * b0 + a0 * b1) << 16 + a1 * b1.
note: All the multiplications here are for 16 bit numbers (a0,a1,b0,b1) with the result as 32 bit number casted into 64 bit (the bigger 32 bits of the number are 0).

Conversion from int to unsigned short [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
this is a part of a C program that i didn't understand :
unsigned short twittlen;
int x;
x = atoi(argv[1]);
twittlen = x;
if(twittlen >= 64) {
printf("Nope , You don't know about Integer");
return -1;
}
if (x >= 64 )
printf("you got it ");
My problem is how to find an int that is greater than 64 but when converting it to unsigned short it will be less than 64 !
I looked alot about limit of those types of integers even on stackoverflow but i didn't find the answer about this !
Thanks in advance :)
When you assign an int value to an unsigned short, truncation will occur.
Assuming int is 4 bytes and short is 2, you simply need to provide the program with a value greater than 64 whose lower two bytes are less than 64.
The maximum unsigned 16-bit value is 65535:
0x0000FFFF
So if you enter the number 65536:
0x00010000
When truncated to just two bytes:
0x0000
Is zero.

Convertion from a uint16 to uint8 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Does anybody know how I can do a correct conversion from a uint16 to a uint8 in a C program for my STM32F091?
I thought that it was like this:
uint16_t Test = 0x565;
uint8_t Test2 = (uint8_t)Test;
But that doesn't work very well, so does anyone has a suggestion?
[update from comment:]
I use a potentiometer that has a minimum value of 0x0 and a maximum value of 0xFFF. Test must have the value of ADC1->DR (this is a value from the ADC in the STM32F0)
Edit:
You cannot compress the data without information lost in this case. Since your data range from ADC is 0x000 to 0xFFF - there is no way for you to represent the data perfectly without information/precision lost in uint8 byte whose range is only from 0x00 to 0xFF
But you still can map the data with precision lost. For example:
uint8_t Test2 = (unsigned)(Test >> 4);
Will cause you to map every 16 range to a single value:
0-15 -> 0
16-31 -> 1
32-47 -> 2
//and so on
Since you only have 8 LEDs anyway, you could use uint8 byte to control the LED - though you will lose precision of the real value, but you are still able to represent the range of the value (per unit of 16) correctly with your 8 LEDs (since you only have 8 LEDs anyway)
Original:
The case is very likely cause by overflow when you cast bigger-sized uint16 data type to smaller-sized uint8:
uint16_t Test = 0x565;
uint8_t Test2 = (uint8_t)Test; //Test2 value is only 0x65, 0x500 is missing
The casting causes the most significant byte value (0x500) to be truncated.

Byte manipulation does not work with unsigned char [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I tried to experiment with bit manipulations on a byte.
At first I tried to say that I have 1111 1111 (256) and 1000 0000(128).
So I do this:
printf("%u\n", 256 & 128);
I expect to get 128 but I get 0.
So I tried:
printf("%u\n", ((unsigned char) 256) & ((unsigned char) 128));
But that gives me the same result.
What is wrong with that?
1111 1111 is 255
So try
printf("%u\n", 255 & 128);
^^^
Take into account that the type of the integer constants 255 and 128 is int.

Byte testing between user specified boundaries [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I'm playing around attempting to check specific bytes within a packet's payload and test if they are >,<,!, = to a specified value. The boundary points are dynamic, what is the best way to evaluate the bytes between them?
For example, I have a packet with a payload (following the headers of course) and I want to evaluate between byte 5 and byte 10 to see if it is greater than some specified value.
The return value of memcmp() does a nice unsigned little endian compare.
return memcmp(&packet[IndexLo], &Reference, IndexHi - IndexLo + 1);
A portable method would simple compare 1 byte at a time.
Quick method but has a number of assumptions:
. Packet data and platform same endian and Little.
. Boundary_width <= sizeof inttype.
. unsigned arithmetic.
. Optimized for Packet_CompareMask().
. Accessing a width integer on any byte boundary OK.
. OK to access memory just past end of packet.
typedef uint64_t inttype;
int Packet_CompareMask(const char *packet, size_t IndexLo, unint64_t Mask, inttype Reference) {
// This fails on machine with alignment restricts on wide integers.
inttype x = *((inttype *) &packet[IndexLo]);
x &= Mask;
if (x < Reference) return -1;
return x > 0;
}
int Packet_CompareRange(const void *packet, size_t IndexLo, size_t IndexHi, inttype Reference) {
inttype Mask = 0;
ssize_t Diff = IndexHi - IndexLo;
if ((Diff <= 0) || (Diff > (sizeof(Mask) - 1))) {
; // handle error
}
// This only works on little endian machines. A variant would work with Big endian.
while (--Diff >= 0) {
Mask <<= 8;
Mask |= 0xFF;
}
return Packet_CompareRange(packet, IndexLo, Mask, Reference);
}

Resources