Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Get n msb and lsb bits

Status
Not open for further replies.

lh-

Member level 1
Joined
Oct 5, 2016
Messages
37
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
277
hi

i'm writing a program in c++ where i have registers. they are 8 bit each. however, i may have values bigger than 255, in which case i need to place the value in say 2 registers if it has 16 bits, 3 registers for 24 bits etc.

so i need to extract first 8 bits (which for me are msb) and second 8 bits (lsb).

getting lsb didn't require me much of thinking:
Code:
register = value & 0x00FF

for msb, this is what i come up with:
Code:
register = (value & 0xFF00) >> 8;

it works but i'm here to ask if there's a better solution.

thanks
 

Re: get n msb and lsb bits

Convert to 16-bit pointer and read as array. Also, you can use union. But compilied assembly will be equal I beileve.
 

Re: get n msb and lsb bits

hmm. are those 2 really better solutions? i mean, bitwise operations are much faster than any other operation, let alone a for loop executing 16 times.

and i still don't get what is the purpose of this array or union. my register storage is itself an array of bytes.
 

Re: get n msb and lsb bits

and i still don't get what is the purpose of this array or union. my register storage is itself an array of bytes.

As Easyrider83 mentioned, by handling the variable of 2 bytes length as an array, you can read one byte without the need of any shift opperation, as for example if you declare this:

Code:
uint8_t Variable[2];
uint16_t * ptr = Variable;
*(ptr) = 0x1234

In theory, you should be able to read its 8 MSB by something like that (not tested):

Code:
uint8_t Value_msb = *(ptr); // 0x12

And to read the 8 LSB, by something like that:

Code:
uint8_t Value_lsb = *(ptr+1); // 0x34
 

Re: get n msb and lsb bits

No need to declare new pointer. Just pointer-type conversion which takes no code :)
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top