Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

efficient way to convert 16 bit value to 8 bit value

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.

like image 279
saurabh agarwal Avatar asked Jul 24 '15 05:07

saurabh agarwal


People also ask

Can you convert 8bit to 16bit?

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.

Is 8 bits or 16 bits better?

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.

What is the range of 16 bits?

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.


1 Answers

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

like image 135
Amol Saindane Avatar answered Oct 04 '22 12:10

Amol Saindane