|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
I have a float that I need converted to a 4-byte char-array.
using sprintf does not work. What do you suggest?
__________________
________________________________________________ "I'm not young enough to know everything." - Oscar Wilde |
| Sponsored Links |
|
|
|
|||
|
Thank you, memcpy will do it, although I have to change the float into a data type that can be unsigned (actually, in Visual Studio 2008, you can declare a float as unsigned, but that's wierd I think).
I have another question though, I think I'll just ask it in this thread for now and just hope that someone sees it ![]() How do you do bitwise operations in C/C++? If I for example want to increase a variable by a binary 1? Or if I want to turn a variable into it's 2's complement (You probably already figured this has something to do with my previus question )
__________________
________________________________________________ "I'm not young enough to know everything." - Oscar Wilde |
|
|||
|
ofc I know that a binary number will increase if you add 1 to it...I know the binary system as well as I know the decimal, hexadecimal and octal. But that is not really the answer to my question I'm afraid.
The question here wasn't how to add 1 to it, but _how_ to do binary operations (I.e. what is the _syntax_). Increasing a value was just an example. Ok, so another example. (I'm still looking for the syntax..) Say I have an 8-bit variable that I want to binary add with 01010101? Or I want to NOT (invert, 1's complement) it?
__________________
________________________________________________ "I'm not young enough to know everything." - Oscar Wilde |
| Sponsored Links |
|
|
|
|||
|
Quote:
Code:
c = a + b; /* "binary" addition */ c = a + b; /* "decimal" addition */ c = a + b; /* "octal" addition */ c = a + b; /* "hexadecimal" addition */ |
|
|||
|
for c/c++ you can use >> or << for right and left bit shifts respectively. To do a certain number of bits give it as an argument after the operators. e.g.
Code:
int y = 4; int x; x = y << 2; |
|
|||
|
Quote:
I'm programming MCU's, mostly 8-bit MCU's from Microchip. Many times I want to do the operations using the binary representation to get a clear and neat code. An example, maybe this will make you understand... On a specific MCU (the PIC16F88) i have a total of 16 I/O pins, divided into two 8-bit ports. In a specific application ihave several sensors connected to one port, so each pin status depends on the status of the sensor connected to that pin. To the other port some output devices are connected, like LED's. I want to compare the pins of one port to a predetermined state, and based on the result do some changes to the other port (I.e: set of 8 pins.) Ie i don't want to compare each pin to a predetermined state, I wan't to compare all 8 pins (the port) at the same time. If ALL of them matches my predetermined state, then I do something.. For clarity purposes, I want to do that comparision using binary representation. Say that unless the status of the 8 pins are (binary) 10101010, the program will do nothing. Using decimal representation just isn't practical. It will make the code unclear. Using binary representation would make it very easy to know what pin should be what state, since each bit corresponds to a physical pin (and in the extent, a physical sensor). Using decimal, octal, hexadecimal, or any other imaginable nummerical system will just not be clear to anyone looking at the harware and firmware code. "If (PORTA == 10101010)" Is a lot more clear than writing "If (PORTA == 170)" or "IF (PORTA == AA)", or "IF (PORTA == 252)" Then, based on the results from the comparision, I might want to change state of pin 5 and 8 (IE bit 5 and 8) on PORTB. Changing the state of a pin is usually done by XOR'ing it with 1 (at least when using ASM, since that lets me change the state of it using only one instruction). So, to alter the state of pin 6 and 8, I would XOR the port with 10010000. Using the decimal representation, that would be 144...very un-intuive and unclear! Do you understand what I mean now?
__________________
________________________________________________ "I'm not young enough to know everything." - Oscar Wilde |
|
|||
|
Quote:
__________________
________________________________________________ "I'm not young enough to know everything." - Oscar Wilde |
|
|||
|
Quote:
Quote:
Bit shifting and bitwise operations XOR: Code:
PORTB ^= 0x90; /* flip bits 7 and 4 (bits 8 and 5 to the new) */ Last edited by dcs; 03-21-2008 at 02:48 PM. |
| Sponsored Links |
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| I need help please! | zizimetalique | C and C++ | 5 | 11-17-2007 10:39 PM |
| Converting [willing to pay] | DarkWalk | General Programming | 1 | 09-28-2007 04:25 PM |
| Missing "0"s when converting hex to byte | lwings | C# Programming | 0 | 05-02-2007 09:43 PM |
| Converting DB values into strings | ashleysmithd | Pascal/Delphi | 5 | 04-24-2007 12:21 PM |
| Converting between ebcdic and ACSII | dirkfirst | General Programming | 3 | 12-30-2006 02:03 PM |
| WingedPanther | ........ | 2753.6 |
| Xav | ........ | 2704 |
| Brandon W | ........ | 1702.32 |
| John | ........ | 1207.73 |
| marwex89 | ........ | 1175.24 |
| morefood2001 | ........ | 966.05 |
| dcs | ........ | 655.75 |
| Steve.L | ........ | 475.59 |
| orjan | ........ | 418.58 |
| Aereshaa | ........ | 383.54 |