Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

simple bit shift

Posted on 2005-02-28
2
Medium Priority
?
266 Views
Last Modified: 2006-11-17
Hi,

Is there any simple bitshift I can use to isolate each digit of a number?

int nTheNumber = 3651;

int nThousands = 3;
int nHundreds = 6;
int nTens = 5;
int nOnes = 1;

I just want to isolate each digit of the number, thanks.
0
Comment
Question by:AlreadyGoogled
2 Comments
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 100 total points
ID: 13419186

You can't "divide by 10" with simple bit shifts, but you can isolate each digit with the C modulo function.

int nTemp;
int nTheNumber = 3651;
int nOnes;
int nTens;
int nHundreds;
int nThousands;


nTemp = nTheNumber;
nOnes = nTemp % 10;
nTemp = nTemp / 10;

nTens = nTemp % 10;
nTemp = nTemp / 10;

nHundreds = nTemp % 10;
nTemp = nTemp / 10;

nThousands = nTemp % 10;
nTemp = nTemp / 10;


Kent
0
 

Author Comment

by:AlreadyGoogled
ID: 13419321
Thanks Kent.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question