Solved

# Unsigned Long Int to Byte?

Posted on 2009-05-03
755 Views
Hi, I have an unsigned long int number (only 0 and 1) and I would like to take the first 8 digits and put them in a byte. Example:

unsigned long int code = 01100011001110;   //13 digits

char byte = ---8 first digits from code---

---remove the 8 first digits from code and place the rest of them at the beggining---

printf("%ld", code);  //this will printf "001110"  (the last 6 digits)

Thanks
0
Question by:AL3X2
• 4
• 2

LVL 40

Expert Comment

You mean, like this?
``````#include <stdio.h>

int main(int argc, char **argv)

{

unsigned long ul = 0x18CE; // 01100011001110

unsigned char uc = static_cast<unsigned char>(ul >> 6);

printf("0x%04X\n", uc); // 01100011 (0x0063)

printf("0x%04X\n", (ul & 0x3F)); // 001110 (0x000E)

}
``````
0

LVL 40

Accepted Solution

evilrix earned 50 total points
Sorry, same thing with a C and not C++ style cast :)
``````#include <stdio.h>

int main(int argc, char **argv)

{

unsigned long ul = 0x18CE; // 01100011001110

unsigned char uc = (unsigned char)(ul >> 6);

printf("0x%04X\n", uc); // 01100011 (0x0063)

printf("0x%04X\n", (ul & 0x3F)); // 001110 (0x000E)

}
``````
0

Author Comment

But, this will work only when ul is 13 digits?
0

Author Comment

Nevermind, I think I found it ;)

Just one more thing.

If i have ul = 1001 (for example), how can I add digits ?

something like  ul = ul + 100, so printf("%ld", ul) will show "1001100" ?
0

LVL 40

Expert Comment

>> But, this will work only when ul is 13 digits?
It is specific to the requires you stated. Line 6 cuts off the fist 6 bytes of the UL using the right shift operator and casts the return into a char and the result is printed at line 8.

01100011001110 >> 6 == 01100011

Line 9 uses the mask 111111 to ignore all but the first 6 bits of the UL

01100011001110 & 111111 == 001110

Once you understand how these 2 operations work you can tailor them to various needs.

0

LVL 40

Expert Comment

>> If i have ul = 1001 (for example), how can I add digits ?
>> something like  ul = ul + 100, so printf("%ld", ul) will show "1001100" ?

Read up on the << and >>  and the & and | bitwise C operators

http://www.cplusplus.com/doc/tutorial/operators/

``````#include <stdio.h>

int main(int argc, char **argv)

{

unsigned long ul = 0x09; // 1001

ul <<= 3; // 1001000

ul |= 4; // 1001100

printf("0x%04X\n", ul); // 1001100 (0x4C)

}
``````
0

## Featured Post

### Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.