473,382 Members | 1,583 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,382 software developers and data experts.

small bit shifting q

ben
hiyer,

say you've got a 32 bit int - is there a nifty way to shift that whole
value right by 4 bits and at the same time collect the 4 bits that are
being shifted off the edge to the right into another variable? i'm
hoping for a way to do it in one foul swoop ... ? probably not, but
just wondering if there's a better way than the obvious :

x = 0xf & bits;
bits >>= 4;

thanks, ben.
Nov 14 '05 #1
4 1645
On Thu, 04 Mar 2004 23:10:58 +0000, ben <x@x.x> wrote:
hiyer,

say you've got a 32 bit int - is there a nifty way to shift that whole
value right by 4 bits and at the same time collect the 4 bits that are
being shifted off the edge to the right into another variable? i'm
hoping for a way to do it in one foul swoop ... ? probably not, but
just wondering if there's a better way than the obvious :

x = 0xf & bits;
bits >>= 4;
I'd have to agree with "probably not". You could do it in one statement by
employing severe operator abuse (of the comma operator), but it wouldn't
gain you anything. In fact, I'll make a bold prediction: if anyone /does/
come up with a way to do it more efficiently, even then the total CPU time
that would be saved by the "improved" version would be dwarfed by the
amount of brain-time spent in pondering the question ;-)
-leor

thanks, ben.


Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #2
ben
In article <ce********************************@4ax.com>, Leor Zolman
<le**@bdsoft.com> wrote:
On Thu, 04 Mar 2004 23:10:58 +0000, ben <x@x.x> wrote:
hiyer,

say you've got a 32 bit int - is there a nifty way to shift that whole
value right by 4 bits and at the same time collect the 4 bits that are
being shifted off the edge to the right into another variable? i'm
hoping for a way to do it in one foul swoop ... ? probably not, but
just wondering if there's a better way than the obvious :

x = 0xf & bits;
bits >>= 4;


I'd have to agree with "probably not". You could do it in one statement by
employing severe operator abuse (of the comma operator), but it wouldn't
gain you anything. In fact, I'll make a bold prediction: if anyone /does/
come up with a way to do it more efficiently, even then the total CPU time
that would be saved by the "improved" version would be dwarfed by the
amount of brain-time spent in pondering the question ;-)


ok fair enough :) it just seemed slightly logical that there might be a
way to do that in one go. not to worry - thanks for confirming.

ben.
Nov 14 '05 #3
ben wrote:
hiyer,

say you've got a 32 bit int - is there a nifty way to shift that whole
value right by 4 bits and at the same time collect the 4 bits that are
being shifted off the edge to the right into another variable? i'm
hoping for a way to do it in one foul swoop ... ? probably not, but
just wondering if there's a better way than the obvious :

x = 0xf & bits;
bits >>= 4;

thanks, ben.


This is a common technique in assembly languages, to rotate
a bit from one number to another for multiprecision numbers.

I haven't seen any assembly languages that offer this capability.
Most will shift a bit into carry and shift in a bit from carry.
I've had to repeat this process in a loop. But there are
better methods.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 14 '05 #4
"Thomas Matthews" <Th****************************@sbcglobal.net> wrote in
message news:0D******************@newssvr31.news.prodigy.c om...
ben wrote:
hiyer,

say you've got a 32 bit int - is there a nifty way to shift that whole
value right by 4 bits and at the same time collect the 4 bits that are
being shifted off the edge to the right into another variable? i'm
hoping for a way to do it in one foul swoop ... ? probably not, but
just wondering if there's a better way than the obvious :

x = 0xf & bits;
bits >>= 4;

thanks, ben.


This is a common technique in assembly languages, to rotate
a bit from one number to another for multiprecision numbers.

I haven't seen any assembly languages that offer this capability.
Most will shift a bit into carry and shift in a bit from carry.
I've had to repeat this process in a loop. But there are
better methods.


The 32-bit IBM Mainframe (S/390) has a double-register shift
that is used for rotating the bits within a 32-bit register.
The shifted-out bits go into an adjacent register,
instead of the bit-bucket. The next instruction is a
bitwise OR that copies the shifted-out bits back into the
source register at the other end. Rotating a 32-bit operand
requires 2 adjacent registers (even/odd numbered) and 3
instructions (including one instruction to clear the register
receiving the shifted-out bits).

The 64-bit IBM Mainframe (z/Architecture) added a rotate
instruction for both 32-bit and 64-bit operands, so the
double register shifting technique is no longer needed
(but it still works for 32-bit values). One instruction to
rotate the bits, and the rotated value can land in a different
target register without altering the source register.

Having said that, I cannot think of a C idiom that
would represent succinctly such an operation of
rotating bits (or extracting bits and shifting the
other bits) for a 32-bit quantity. The best I can
think of is the OP's example.
--
----------------------------
Jeffrey D. Smith
Farsight Systems Corporation
24 BURLINGTON DRIVE
LONGMONT, CO 80501-6906
http://www.farsight-systems.com
z/Debug debugs your Systems/C programs running on IBM z/OS!
Are ISV upgrade fees too high? Check our custom product development!
Nov 14 '05 #5

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

Similar topics

6
by: David Stockwell | last post by:
Hi, My background is c/c++ and java. I'm learning python at this point. My question is does python share java's peculiar mode of bit shifting, or does python adhere closer to c's bit shifting?...
9
by: GGG | last post by:
Noticed something odd in the way bit shifting was working today. As far as I have ever heard, shifting will shift in zeros(signed ints aside) However I foudn something odd when I am shifting...
2
by: salsipius | last post by:
Can someone please help me clarify the below code. I think the shifting has to do with converting datatypes and/or loss of data but am not really clear on the details, could you help shed some...
10
by: krunalb | last post by:
Hi, I am trying to shift unsigned long long value by 64 bits and this is what i get #include <stdio.h> int main() { unsigned short shiftby= 64;
20
by: Charles Sullivan | last post by:
I understand different processor hardware may store the bits in a byte in different order. Does it make a difference in C insofar as bit-shifting unsigned char variables is concerned? E.g, if I...
169
by: JohnQ | last post by:
(The "C++ Grammer" thread in comp.lang.c++.moderated prompted this post). It would be more than a little bit nice if C++ was much "cleaner" (less complex) so that it wasn't a major world wide...
16
by: lak | last post by:
i know left and right shift normally,but i cant know what happens if it is negative. for example int x=-2; x<<=1;//what happens here
4
by: Neil | last post by:
I previously posted about data shifting between records in my Access 2000 MDB with a SQL Server 7 back end, using ODBC linked tables. Every once in a while, data from one record mysteriously...
12
by: Boltar | last post by:
I seem to be having yet more wierd issue with bit shifting. It seems the following code doesnt do anything under gcc (ie it returns -1 as both results). Anyone know why? Is it another language...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.