10

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.

3
  • Deleting my answer, I missed that variables are unsigned.
    – Mohit Jain
    Jul 24, 2015 at 5:53
  • So, for unsigned type . It would be platform dependent and for signed type it would not ? Jul 24, 2015 at 5:57
  • 2
    The behavior (in your question's code) is well defined for unsigned types. For signed types, things get more tricky to answer, due to the sign bit.
    – Thanatos
    Jul 24, 2015 at 5:58

4 Answers 4

8

While both answers are correct, the bit masking here is completely redundant. It happens implicitly when converting to uint8_t. Without exactly sized integer types (and, speaking of performance, you should consider that, because performance is in general best when using the native word size of the machine), this would be different:

unsigned int val = 0xabcd;
unsigned int vr = val & 0xff;
assert(vr == 0xcd);

But when you really need to have these exactly sized type, the best code is IMHO

uint16_t val = 0xabcd;
uint8_t vr = (uint8_t) val;

The explicit cast is here to document the intention! Without it, a good compiler will warn you about the implicit conversion possibly losing information (and you should always enable compiler warnings, e.g. for gcc -Wall -Wextra -pedantic, to catch cases where you do such a conversion by accident).

The performance of all variants using the exactly sized types should be the same, because a decent compiler will emit the same code for all of them. The version using just unsigned int might perform a bit better.

[edit]: As you're asking about memory performance, too: it is unlikely that you gain something by using uint8_t because some architectures require values smaller than the native word size to be aligned to word boundaries. If they don't require it, it might still be faster to have them aligned, so the compiler might decide to do so. That just introduces unused padding bytes. With gcc, you can use the option -Os to optimize for size and as x86 architecture is byte-addressable, this may result in uint8_t being used without padding on a PC, but consequently with lower speed. Most of the time, speed vs memory is a tradeoff, you can have either one or the other.

6

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

7
  • 3
    From performance (memory and speed ) perspective, how is it different from "vr=val" ? Jul 24, 2015 at 5:42
  • 4
    @Amol: That is not true. For unsigned types, as the OP is using, the behaviour is specified by the standard.
    – caf
    Jul 24, 2015 at 5:51
  • @caf : usually it is not good programming practice to do direct assignment of 16 bit to 8 bit variable directly, i will check more info regarding the reason behind it. I totally agree with you though as it is unsigned Jul 24, 2015 at 5:54
  • 1
    @saurabhagarwal: Your question was about unsigned types. Signed types are a different beast.
    – Thanatos
    Jul 24, 2015 at 5:57
  • 2
    You may as well do vr = (uint8_t)val (which is exactly what OP is doing, except for the lack of an explicit cast (which in the worst case would yield a compilation warning, but in either case would not yield any runtime impact)). Jul 24, 2015 at 7:13
2

you can use the bit-masking concept.
Like this,

uint16_t val = 0xABCD;
uint8_t vr  = (uint8_t) (val & 0x00FF);

Or this can also be done by simply explicit type casting, as an 8-bit integer only carries LBS 8-bits from 16-bits value, & discards the remaining MSB 8-bits (by default, when assigns a larger value).

3
  • The cast is not necessary.
    – alk
    Jul 24, 2015 at 5:53
  • Anyways, the cast is not needed as the bitwise AND with the assign is specified behavior. Jul 24, 2015 at 5:55
  • @alk, Yes you are right. but it is a good programming practice to understand. I also right this in my answer with the word "By Default". Jul 24, 2015 at 5:56
0

While the above answers are correct, here an other way to do it. This method does not require bitwise operations

#include <stdint.h>
#include <stdio.h>

union converted16{
        struct{
                uint8_t lsb;
                uint8_t msb;
        }raw;
        uint16_t data;

}mynumber;

int main(int argc, char *argv[]){

        mynumber.data = 0xFEAB;
        printf("msb = %u, lsb = %u\n",mynumber.raw.msb, mynumber.raw.lsb);
        return 1;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.