473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Bitwise right shift

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

Jul 25 '07 #1
5 4941
la********@yahoo.com wrote:
>[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?
Jul 25 '07 #2
On Jul 25, 5:37 pm, Mark Bluemel <mark_blue...@pobox.comwrote:
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?

Jul 25 '07 #3
la********@yahoo.com wrote:
On Jul 25, 5:37 pm, Mark Bluemel <mark_blue...@pobox.comwrote:

>>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?
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.
Jul 25 '07 #4
la********@yahoo.com wrote:
On Jul 25, 5:37 pm, Mark Bluemel <mark_blue...@pobox.comwrote:
>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?
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.

Jul 25 '07 #5
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.

Jul 26 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bob | last post by:
What is an equivalent of a Right Shift operator (>>) in visual basic 6.0? I am trying to convert a C++ code into visual basic that looks like the following: While (m < n) { k = (m+n) >> 1 } ...
3
by: ERE | last post by:
Pardon me if this has been asked a million times -- but I thought that because of generics, the right shift operator was supposed to change with C# 2.0. I thought it was supposed to change from...
12
by: Mick_fae_Glesga | last post by:
OK, the solution to this is probably blindingly obvious to everyone, but... surely it can't be right. I am compiling with borland bcc32 free compiler this piece of code is designed to identify...
16
by: Santosh Nayak | last post by:
Hi, Is there any way to catch the losing bit occurring due to Right Shift Operator ? e.g int a = 5 ; a = a >1 ; // // a is now 2 and the least significant bit is lost // // I want this...
4
by: G Iveco | last post by:
I am using this type of code to do right-shifting, B = 3; data1 = (data + (1 << (B-1))) >B; data1 seems incorrect when data = -4-8*i.. which means it rounds -1.5 to -1 instead of -2. On...
0
by: madhubangaramakki | last post by:
Please let me know with a example : How does a right shift operator works for long integer in python?
2
by: gaya3 | last post by:
Hi, can anyone explain the unsigned right shift operator with example? And also why there is no unsigned left shift operator? is there any specific reason for that? -Thanks & regards, Hamsa
1
by: Anne | last post by:
Hi, How can I in a MDE-file disable the "startup with Right Shift Key" ? I tried it in Options --Startup, but can't get it working. Thanks, Anne
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.