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

bitwise..

Hi
C++ should provide us bit shift but fill with 1 rather than 0.
Or i am wrong, c++ have it???

if 1 left shift 2, i want 111 rather than 100

thanks
from Peter (cm****@hotmail.com)

Oct 4 '05 #1
5 1662
cm****@hotmail.com wrote:
Hi
C++ should provide us bit shift but fill with 1 rather than 0.
Or i am wrong, c++ have it???

if 1 left shift 2, i want 111 rather than 100


I am afraid that best you can do is something like (x & 1) | (x << 1).
AFAIK, most CPUs do not have single instruction for this anyway....

Mirek
Oct 4 '05 #2
cm****@hotmail.com wrote:

Hi
C++ should provide us bit shift but fill with 1 rather than 0.
Why 'should'?
Or i am wrong, c++ have it???


Not everything you or I will ever need os provided by the language. But the
language is powerful enough that you can synthesize what you need from what
is existing.

result = ( value << 1 ) | 0x01;

does what you want. First shift one position to the left, then
set the leftmost bit to 1

For 'shift values' other then 1, I think the easieast approch is
to use a lookup table:

unsigned int MyShiftLeft( unsigned int What, unsigned int How )
{
static unsigned int OnePattern[] = { 0, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F };

return (What << How ) | OnePattern[How];
}

This works for values of 'How' up to 7. If that is not enough, enlarge the table.

--
Karl Heinz Buchegger
kb******@gascad.at
Oct 4 '05 #3

<cm****@hotmail.com> schrieb im Newsbeitrag
news:11**********************@o13g2000cwo.googlegr oups.com...
Hi
C++ should provide us bit shift but fill with 1 rather than 0.
Or i am wrong, c++ have it???

if 1 left shift 2, i want 111 rather than 100

thanks
from Peter (cm****@hotmail.com)


Maybe like this?

inline unsigned int ASL_FILL(unsigned int num, int sh)
{
return (num<<sh) | ((1<<sh)-1);
}

int main()
{
int test = 0x20;
test = ASL_FILL(test, 3);
}

Oct 4 '05 #4
<cm****@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
C++ should provide us bit shift but fill with 1 rather than 0.
Or i am wrong, c++ have it???

if 1 left shift 2, i want 111 rather than 100


To shift x to the left n bits, filling with 1, you can write ~(~x << n)

Oct 4 '05 #5
Mirek Fidler wrote:
cm****@hotmail.com wrote:
Hi
C++ should provide us bit shift but fill with 1 rather than 0.
Or i am wrong, c++ have it???

if 1 left shift 2, i want 111 rather than 100

I am afraid that best you can do is something like (x & 1) | (x << 1).
AFAIK, most CPUs do not have single instruction for this anyway....

Mirek


Oops, sorry, I thought you want to copy LSB... (like arithemtic shift in
inverse direction).

Mirek
Oct 4 '05 #6

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

Similar topics

6
by: jas_lx | last post by:
The basic understanding of what bitwise operators (& ^ | >> << ) comes fairly simple, as long as one has a fundamental understanding of bits, bytes and binary. Having done some Win32...
2
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
8
by: Paul E Collins | last post by:
Suppose I have a few Keys objects: Keys k1 = Keys.V; // V Keys k2 = Keys.Control | Keys.V; // Ctrl+V Keys k3 = Keys.Shift | Keys.J; // Shift+J I need to determine which of these include the...
9
by: Christopher Weaver | last post by:
I know that the bitwise AND of 8 and 4 will return 0 or false and the bitwise AND of 8 and 9 will return 1 or true but I don't know how to write the synax for it in C#. I have a value that ranges...
5
by: noridotjabi | last post by:
I'm learning to program in C and any tutorial or book that I read likes to briefly touch on birdies operators and then move on without giving any sort of example application of them. Call me what...
2
by: Mark Rae | last post by:
Hi, This isn't *specifically* an ASP.NET question, so I've also posted it in the ADO.NET group - however, it's not too far off-topic... Imagine a SQL Server 2005 database with a table with an...
3
by: Jay Ruyle | last post by:
I'm trying to figure out a way to list several items in a listbox and let the user select any number of items in the listbox. I have tried to code in the items as bitwise items but all it stores...
5
by: Gigs_ | last post by:
Can someone explain me bitwise expression? few examples for every expression will be nice x << y Left shift x >y Right shift x & y Bitwise AND x | y Bitwise OR x ^ y Bitwise XOR (exclusive...
45
by: Carramba | last post by:
Hi! I now that I can't do straight forward any bitwise operation on float (double etc..). But I wondering what is the easiest/best way to do this? I was thinking if I have float x=1.1111 so I can...
29
by: Carl Banks | last post by:
Anyone with me here? (I know the deadline for P3 PEPs has passed; this is just talk.) Not many people are bit-fiddling these days. One of the main uses of bit fields is flags, but that's not...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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.