?
Solved

simple bit shift

Posted on 2005-02-28
2
Medium Priority
?
260 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

762 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