473,563 Members | 2,732 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1659
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.co m> 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.l earn.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******** **********@news svr31.news.prod igy.com...
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
4384
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? in java there are 3 kinds of bit shifts: << (shift left) >> (preserve the sign bit as we move right ) >>> (0 filled on the left as we...
9
8702
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 exactly the number of bits in a value... i.e. uint32_t x = 5;
2
2043
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 light please? // Allocate array for( i = 0; i < Length; i++ ) { //pArray_00 is a BYTE Array -- Here a cast is used because
10
10672
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
2485
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 have unsigned char x = 1; is it always true that (x << 1) == 2 (x << 2) == 4 etc?
169
8959
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 untaking to create a toolchain for it. Way back when, there used to be something called "Small C". I wonder if the creator(s) of that would want to...
16
1542
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
1858
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 appears in another record. This incident happened again, this time adding a new wrinkle to the situation. There are two tables -- TableA and TableB --...
12
2240
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 definition or CPU issue? main() { printf("%d\n",(int)0xFFFFFFFF >1); printf("%d\n",(int)-1 >1); }
0
7664
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7583
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7885
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7638
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7948
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6250
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
923
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.