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

shift question

Hi there!

I have a very silly shift operation question:

My test code does something like this:
int j=1;
int y=0;
int i=0;
int bs=0;

for(i=0;i<16;i++){
bs=1<<j;
y=?????;
printf("%i:\tj=%i\tbs=%i\t\ty=%i\n", i, j, bs, y);
j++;
}

I want y to be the bit set in bs=1<<j - I know usally done by y=j...

But in my real application I have a bs=1024 or bs=512 and have to
determine the y...

Thanx a lot!
Johann

Apr 16 '06 #1
3 1793
Ico
horvatj <ho***********@gmx.net> wrote:

My test code does something like this:
int j=1;
int y=0;
int i=0;
int bs=0;

for(i=0;i<16;i++){
bs=1<<j;
y=?????;
printf("%i:\tj=%i\tbs=%i\t\ty=%i\n", i, j, bs, y);
j++;
}

I want y to be the bit set in bs=1<<j - I know usally done by y=j...

But in my real application I have a bs=1024 or bs=512 and have to
determine the y...


Your question is not very clear, but I assume that what you are looking
for is the base-2 logarithm of a number. If performance is not a big
issue, you can use the log() function from your local math library :

#include <math.h>

...

int x = 512;
int y;

y = log(x) / log(2);

Another method would be to look for the first bit that is set to 1 with
a simple loop (assuming 16 bit integers, like in your example) :

int x = 512;
int y;

for(y=15; y>=0; y--) if(x & (1<<y)) break;

or something like :

int x = 512;
int y = 16;
int mask = 1<<16;

while(!(x & mask)) {
mask >>= 1;
y --;
}

This can be further optimized by using binary search methods. If you are
really interested, you can find more recipes in the book "Hacker's
Delight", chapter 5-3.
--
:wq
^X^Cy^K^X^C^C^C^C
Apr 16 '06 #2
"Ico" <us****@zevv.nl> wrote in message
news:44**********************@dreader30.news.xs4al l.nl...
horvatj <ho***********@gmx.net> wrote:

My test code does something like this:
int j=1;
int y=0;
int i=0;
int bs=0;

for(i=0;i<16;i++){
bs=1<<j;
y=?????;
printf("%i:\tj=%i\tbs=%i\t\ty=%i\n", i, j, bs, y);
j++;
}

I want y to be the bit set in bs=1<<j - I know usally done by y=j...

But in my real application I have a bs=1024 or bs=512 and have to
determine the y...
Your question is not very clear, but I assume that what you are looking
for is the base-2 logarithm of a number. If performance is not a big
issue, you can use the log() function from your local math library :

#include <math.h>

...

int x = 512;
int y;

y = log(x) / log(2);

Another method would be to look for the first bit that is set to 1 with
a simple loop (assuming 16 bit integers, like in your example) :

int x = 512;
int y;

for(y=15; y>=0; y--) if(x & (1<<y)) break;

or something like :

int x = 512;
int y = 16;
int mask = 1<<16;


Should not this be 15 instead of 16?
while(!(x & mask)) {
mask >>= 1;
y --;
}

Apr 16 '06 #3
Ico
stathis gotsis <st***********@hotmail.com> wrote:
"Ico" <us****@zevv.nl> wrote in message
Your question is not very clear, but I assume that what you are looking
for is the base-2 logarithm of a number. If performance is not a big
issue, you can use the log() function from your local math library :

#include <math.h>

...

int x = 512;
int y;

y = log(x) / log(2);

Another method would be to look for the first bit that is set to 1 with
a simple loop (assuming 16 bit integers, like in your example) :

int x = 512;
int y;

for(y=15; y>=0; y--) if(x & (1<<y)) break;

or something like :

int x = 512;
int y = 16;
int mask = 1<<16;


Should not this be 15 instead of 16?


Indeed, it should.

--
:wq
^X^Cy^K^X^C^C^C^C
Apr 16 '06 #4

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

Similar topics

3
by: Antioch | last post by:
Ok, so Im a newb python programmer and I'm trying to create a simple python web-application. The program is simply going to read in pairs of words, parse them into a dictionary file, then randomly...
44
by: Carlos Andr?s | last post by:
Hi everybody. I've got a problem. I'd like to avoid opening a new window when you have pressed the shift key and you click in the left button of the mouse. I've tried the next solution, in the...
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
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
13
by: HARDCORECODER | last post by:
ok here is the question. I want to exract the first 4 bits in a int so let say int b = somenumber; int result= 0; result = b << 4; if I got this right result should contain the 4 bits that...
1
by: jonathanmcdougall | last post by:
I am developing a calendar on which dates can be clicked and selected. A calendar is a table in which each day is a cell. Each cell has a unique id. By left-clicking on a date, it gets selected...
7
by: gokkog | last post by:
Hello, Recently I have the book Programming Pearls. Nice read! Perhaps it is weekend, I cannot understand the following codes well: #define BITSPERWORD 32 #define SHIFT 5 #define MASK 0x1F...
3
by: Amy Smith | last post by:
Hello there, I am having a small problem which been challenging me for few days and need help or advice. I am trying to calculate the day-shift for employees based on the time they started and...
6
by: saumya.agarwal | last post by:
Hi, I am using libxml2 for xml parsing. When the client application sends data to libxml2 in UTF-8 format, it works fine. But, I have a scenarion in which the client application sends data to...
4
by: hui11 | last post by:
Hi, According to the doc at mozilla, http://developer.mozilla.org/en/docs/DOM:event.charCode, the charCode takes shift into consideration when pressed. I found that to be true for other cases...
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: 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
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...
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:
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
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,...
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
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...

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.