I have a variable that holds 16-bit value. I need only 8 LSB. Rest 8 bits needs to be discarded.
I am using this code for doing this.
#include<stdio.h>
#include<stdint.h>
int main(int argc, char *argv[])
{
int linkIndx,size=128;
uint16_t val = 0xABCD;
uint8_t vr;
vr = val; //this assignment discards upper 8-bits
printf("0x%X 0x%X ", val, vr);
}
Result:
0xABCD 0xCD
I want to know, Is it a good way to take 8 LSB from 16 bit variable?
EDIT:
Please add performance issues (from memory and speed perspective) with this particular way of implementation.
In general there is no way back from 8bit to 16bit. With 8bit images you can have 256 shades of gray, with 16bit you may have 65536 shades of gray. So if you have a 16bit image that shows more than 256 shades of gray and you convert it to 8bit, you definitely loose gray-levels.
The number of bits will define how many possible colors an image can display. An 8-bit image will be able to display a little more than 16 million colors, whereas a 16-bit image will be able to display over 280 trillion colors.
Integer, 16 Bit: Signed Integers ranging from -32768 to +32767. Integer, 16 bit data type is used for numerical tags where variables have the potential for negative or positive values. Integer, 16 Bit Unsigned: Unsigned whole or natural numbers ranging from 0 to +65535.
You can do as below:
uint16_t val = 0xABCD;
uint8_t vr = val & 0x00FF; // Bitwise AND Operation
Using this bitwise operation you will get 8 bits of LSB
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With