Bitwise right shift

L

lancer6238

Hi all,
I'm trying to write a program that extracts the first k bits of 64-bit
double values, given 2^k (which I've defined as size). Here, I use the
example that k = 3.

For example, 101001...011 would return 5, since the first 3 bits are
101.

#include <stdio.h>
#include <math.h>

#define size 8
#define NBITS 64 // number of bits in the datatype

int main()
{
double var, nvar;
unsigned int shift = 0;
int k;

var = pow(2, 63) + pow(2, 61);
k = (int)log2(size);
nvar = var>>(NBITS-k);
printf("nvar = %lf\n", nvar);
return 0;
}

However, I get the error "invalid operands to binary >>". What is
wrong with my program?

Thank you.

Regards,
Rayne
 
M

Mark Bluemel

[snip]
double var, nvar;
unsigned int shift = 0;
int k;

var = pow(2, 63) + pow(2, 61);
k = (int)log2(size);
nvar = var>>(NBITS-k); [snip]

However, I get the error "invalid operands to binary >>". What is
wrong with my program?

What does the standard say are the valid operands to binary shifts?
 
L

lancer6238

What does the standard say are the valid operands to binary shifts?

The operands have to be integers.

So what do I do if I need 64-bit or 128-bit integers?
 
M

Mark Bluemel

The operands have to be integers.

So what do I do if I need 64-bit or 128-bit integers?
Find out if there are such things on your platform?

What are you actually trying to do here? Don't tell us how you are
trying to do it - that's clearly not working, for reasons we've already
established. Tell us about the end result you're looking for.
 
S

santosh

The operands have to be integers.

So what do I do if I need 64-bit or 128-bit integers?

If your implementation supports C99, the type 'long long' is guaranteed to
at least 64 bits. So are the types int64_t, uint64_t, int_least64_t,
uint_least64_t, int_fast64_t, and uint_fast64_t. All the types other than
long long are defined in the stdint.h header.

The types intmax_t and uintmax_t, again in stdint.h, are the largest
supported integer types for a particular implementation. It may or may not
be 128 bits. INTMAX_MIN, INTMAX_MAX and UINTMAX_MAX give the ranges for
these types.

If your implementation doesn't support the integer sizes your want, even as
an extension, using a library like GNU GMP might be another option. If all
else fails, there's always assembler.
 
L

lancer6238

I was actually trying to sort 64-bit or 128-bit integers using
parallel computing. It works for 2^k processors. If there are, say, 4
processors, then all numbers 00xxxxxxxx go to processor 0, numbers
01xxxxxxxx go to processor 1, 10xxxxxxxx go to processor 2, 11xxxxxxxx
go to processor 3. Thus the numbers are already partially sorted in
that all of the numbers in processor 0 are less than all of the
numbers in processor 1, etc. Then I can sort each batch of numbers on
each processor using a
quicksort.

I was trying to extract the first k bits of the integers to determine
which processor they should go to.

santosh: Thank you for your reply.
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top