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.

How do we do slicing and concating of digits in C programming.

Status
Not open for further replies.

sandy3129

Member level 3
Joined
Nov 7, 2014
Messages
56
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Activity points
445
hello friends, how do i do bit slicing and concatenating in C programming, lets suppose i have given inputs int A = 24, int B = 36; the constant A should be represented in first 15 bits of another constant lets suppose C,(from first 0 to 14 bits) and constant B should be represented from C( 16 to 30) of constant C, hence C = x"00240018"; how do i do it in C generically???
 

Re: how do we do slicing and concating of digits in C programming.

First make sure there are enough bits in the variable to hold the result (C) then shift the value for the upper bits by the number of bits to align it with its new location, for example:
C = B << 16;
then OR the result with the lower bits:
C |= A;
the result is in C.

Brian.
 
Re: how do we do slicing and concating of digits in C programming.

Code:
#include <stdint.h>
typedef struct example {
    uint32_t A : 15;
    uint32_t pad1 : 1;
    uint32_t B : 14;
    uint32_t pad2 : 2;
};
This defines a 32b value as you have described. you'll never use pad1 or pad2. but you can use variable.A and variable.B as you describe. good compilers will convert this into mov+and when things line up. or mov+shift+or+and otherwise.

It is also somewhat common to have:
Code:
union moreExample {
  uint32_t safeRaw;
  uint16_t optRaw;
  example base;
};

This allows you to treat the value either as a 32bit blob or as the two values or as the two values without range checks.

You can also use macros. C has a long history with macros for bit manipulation.

Also, keep in mind that C/C++ has implicit conversion rules that can be unexpected.
 
Re: how do we do slicing and concating of digits in C programming.

First make sure there are enough bits in the variable to hold the result (C) then shift the value for the upper bits by the number of bits to align it with its new location, for example:
C = B << 16;
then OR the result with the lower bits:
C |= A;
the result is in C.

Brian.

A is 15 bit word, and B is also a 15 bit word,

#include <stdio.h>
int main(void)
{
int A = 24;
int B = 36;
int C = (A << 16) | B;
printf("%X", C);
return 0;
}

Thanks i got it :)

- - - Updated - - -

Thanks for the reply I prefered mov+shift+or :).
 

Re: how do we do slicing and concating of digits in C programming.

You should always be aware of sign. C/C++ has a lot of quirks that lead to odd corner cases. For example:
Code:
int main(void) 
{
int A = 24;
int B = -36;
int C = (A << 16) | B;
printf("%X", C);
return 0;
}

And now your Mars rover has crashed into Marseille.

--edit, you should use types that describe the size and intent if you are doing bit-manipulation or low level math. except for ptrdiff_t, unless you know how ptrdiff_t actually works.

--edit2: changed name for a closer common prefix. don't actually crash anything into an iconic city....
 
Last edited:

Re: how do we do slicing and concating of digits in C programming.

Thanks for the information, have to study about it, it wont crash, my input constants will always be positive.
 

Re: how do we do slicing and concating of digits in C programming.

it wont crash, my input constants will always be positive.
until they aren't, because someone coded the inputs as signed ;-)
 

Re: how do we do slicing and concating of digits in C programming.

until they aren't, because someone coded the inputs as signed ;-)
I'm the one whose coding everything :-D
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top