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
Medium Priority
266 Views
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

LVL 46

Accepted Solution

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

ID: 13419321
Thanks Kent.
0

## Featured Post

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.
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
###### Suggested Courses
Course of the Month12 days, 13 hours left to enroll