bitwise..

C

cmk128

Hi
C++ should provide us bit shift but fill with 1 rather than 0.
Or i am wrong, c++ have it???

if 1 left shift 2, i want 111 rather than 100

thanks
from Peter ([email protected])
 
M

Mirek Fidler

Hi
C++ should provide us bit shift but fill with 1 rather than 0.
Or i am wrong, c++ have it???

if 1 left shift 2, i want 111 rather than 100

I am afraid that best you can do is something like (x & 1) | (x << 1).
AFAIK, most CPUs do not have single instruction for this anyway....

Mirek
 
K

Karl Heinz Buchegger

Hi
C++ should provide us bit shift but fill with 1 rather than 0.

Why 'should'?
Or i am wrong, c++ have it???

Not everything you or I will ever need os provided by the language. But the
language is powerful enough that you can synthesize what you need from what
is existing.

result = ( value << 1 ) | 0x01;

does what you want. First shift one position to the left, then
set the leftmost bit to 1

For 'shift values' other then 1, I think the easieast approch is
to use a lookup table:

unsigned int MyShiftLeft( unsigned int What, unsigned int How )
{
static unsigned int OnePattern[] = { 0, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F };

return (What << How ) | OnePattern[How];
}

This works for values of 'How' up to 7. If that is not enough, enlarge the table.
 
G

Gernot Frisch

Hi
C++ should provide us bit shift but fill with 1 rather than 0.
Or i am wrong, c++ have it???

if 1 left shift 2, i want 111 rather than 100

thanks
from Peter ([email protected])

Maybe like this?

inline unsigned int ASL_FILL(unsigned int num, int sh)
{
return (num<<sh) | ((1<<sh)-1);
}

int main()
{
int test = 0x20;
test = ASL_FILL(test, 3);
}
 
A

Andrew Koenig

C++ should provide us bit shift but fill with 1 rather than 0.
Or i am wrong, c++ have it???

if 1 left shift 2, i want 111 rather than 100

To shift x to the left n bits, filling with 1, you can write ~(~x << n)
 
M

Mirek Fidler

Mirek said:
I am afraid that best you can do is something like (x & 1) | (x << 1).
AFAIK, most CPUs do not have single instruction for this anyway....

Mirek

Oops, sorry, I thought you want to copy LSB... (like arithemtic shift in
inverse direction).

Mirek
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top