473,473 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Want to shift bits with addition of 1

Hi,

I want to add bits to present number. Shift operator pushes bits and
add '0' at the end.

I want to shift bits and want to add 1 at end

Ex,

1)
suppose Input 0000
for 1 bit I want output as 0001

2)
Input 0100
for 1 bit output should be
1001
any help is welcome.

Thanks

Jan 16 '06 #1
5 12735
LinuxGuy wrote:
Hi,

I want to add bits to present number. Shift operator pushes bits and
add '0' at the end.

I want to shift bits and want to add 1 at end


Thats not a quesion regarding standard C++ !!
Anyways, why not just shift left 1 and add 1 ? i.e. f(x) = (x <<1 )+ 1
Or,still faster, shift left 1 and bitwise or with 1 : i.e. f(x) = (x <<
1) | 1

Jan 16 '06 #2
> Thats not a quesion regarding standard C++ !!

Thats like saying a "how can I loop 3 times" question doesn't concern
the C++ standard!

Jan 16 '06 #3
* LinuxGuy:
Hi,

I want to add bits to present number. Shift operator pushes bits and
add '0' at the end.

I want to shift bits and want to add 1 at end

Ex,

1)
suppose Input 0000
for 1 bit I want output as 0001

2)
Input 0100
for 1 bit output should be
1001
any help is welcome.


The C++ shift operator is '<<'. Bitwise or is '|'. And there you have
it.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jan 16 '06 #4
Neelesh Bodas wrote:
LinuxGuy wrote:

I want to add bits to present number. Shift operator pushes bits and
add '0' at the end.

I want to shift bits and want to add 1 at end


Thats not a quesion regarding standard C++ !!
Anyways, why not just shift left 1 and add 1 ? i.e. f(x) = (x <<1 )+ 1
Or,still faster, shift left 1 and bitwise or with 1 : i.e. f(x) = (x <<
1) | 1


how do you know y = (x << 1) | 1;
is faster than y = = (x << 1) + 1; ?
which implementations did you measure it on?
--
Nick Keighley

Jan 16 '06 #5

"LinuxGuy" <ra**********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hi,

I want to add bits to present number. Shift operator pushes bits and
add '0' at the end.

I want to shift bits and want to add 1 at end

Ex,

1)
suppose Input 0000
for 1 bit I want output as 0001
unsigned int x(0);
x |= 1;

2)
Input 0100
for 1 bit output should be
1001


unsigned int x(4);
x = (x << 1) | 1;

I'm not sure if the parentheses are strictly necessary,
I just used them to ensure order of operations rather
than having to look up precedence rules.

Finally, note that the number of bits used to represent
a given type depends upon the implementation, subject
to minimum requirements, e.g. 'unsigned int' must
have at least 16 bits, but could have more.

-Mike
Jan 18 '06 #6

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

Similar topics

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...
43
by: Mehta Shailendrakumar | last post by:
Hello, Can anyone suggest me operator to perform arithmetic shift in C? May it be for a perticular compiler. Thank you in advance. Regards, Shailendra
4
by: Kevin | last post by:
I was looking through some source code and noticed the used of the C# << operator. Why is this being used here and under what circumstances is an left-shift operator useful. internal enum...
8
by: Rudolf | last post by:
How do you do a bit shift in VB.Net 2002? In VB.Net 2003n you can use the << or >> operators. Thanks Rudolf
11
by: Kenneth Lantrip | last post by:
Anyone got any ideas as to how this process could be improved for speed? this is what I have... Dim j, q As Integer Dim x(16), y(16) As Byte x.CopyTo(y, 0) ' shift left circular 24 bits
4
by: Markus Hahn | last post by:
While coding some binary data handling I found this IMHO strange behavior: Dim bToShift As Byte = 1 bToShift <<= 9 Console.WriteLine("and the value is {0}", bToShift) The output is "2", I...
4
by: sandhya | last post by:
Hello Folks, i hava a problem in coding of circular left shift of 25 bits in my program...how do i perform it, and how do i use unsigned in VB. My program (IDEA algorithm implementation in VB) ...
56
by: Christian Christmann | last post by:
Hi, in the header of my class I've a constant static const int a = ( 1 << 32 ) - 1; When compiling the code, g++ issues the warning "warning: left shift count >= width of type" Why? And...
7
by: L.G | last post by:
I met one problem of shift a number with equal or more than 32 bits. When running this : uint32_t num = 0x45F5F; int shift = 33; uint32_t res = num << shift; printf( "\\exp: %08XH << %d =...
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
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,...
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...
0
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...
0
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.