Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Want to shift bits with addition of 1

Reply
Thread Tools

Want to shift bits with addition of 1

 
 
LinuxGuy
Guest
Posts: n/a
 
      01-16-2006
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

 
Reply With Quote
 
 
 
 
Neelesh Bodas
Guest
Posts: n/a
 
      01-16-2006
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

 
Reply With Quote
 
 
 
 
Shark
Guest
Posts: n/a
 
      01-16-2006
> Thats not a quesion regarding standard C++ !!

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

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-16-2006
* 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?
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      01-16-2006
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

 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      01-18-2006

"LinuxGuy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Left Shift / Right Shift Operators Santosh Nayak C Programming 16 11-30-2006 05:10 PM
shifting bits, shift 32 bits on 32 bit int GGG C++ 10 07-06-2006 06:09 AM
Shift - byte[] buf shift Roberto Gallo Java 3 01-27-2004 04:26 PM
8-Bits vs 12 or 16 bits/pixel; When does more than 8 bits count ? Al Dykes Digital Photography 3 12-29-2003 07:08 PM
left shift then right shift an unsigned int Wenjie C++ 3 07-11-2003 08:22 PM



Advertisments