Marty
Marty

Reputation: 99

How to get the least significant bits of a char in C++

I hava a char variable, and i am only interested in the two least significant hex values. How do I loose al the other values? example:

 char input = 0xFFFFFFD1;  

I want:

 output = 0xD1;    

I tried:

output = (input&0x000000FF);  

but than I just get:

output = 0xFFFFFFD1

How do i solve this challenge?

Upvotes: 1

Views: 1522

Answers (1)

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272667

It is very likely that char is 8-bit on your platform. In which case, this:

char input = 0xFFFFFFD1;

is the same as this:

char input = 0xD1;

I'm also assuming that output is of type int. In which case, you need this:

int output = (unsigned char)input & 0xFF;

Demo: http://ideone.com/FOdRG.

[If this is not the problem, then you will need to update your question to include the actual code you're using.]

Upvotes: 2

Related Questions