.:: Bots United ::.

.:: Bots United ::. (http://forums.bots-united.com/index.php)
-   General Programming (http://forums.bots-united.com/forumdisplay.php?f=25)
-   -   Binary numbers (http://forums.bots-united.com/showthread.php?t=1233)

Lazy 02-04-2004 01:06

Binary numbers
 
This seems a little odd but for some reason I can only do binary operations in inline assembly blocks.

In the following example the function takes a lowercase character and XORs it with 00100000 to convert it to an uppercase character.

Code:

char charupper( char c )
{
if ( c & ( 1 << 5 ) )        // Is bit 5 set?
{
        __asm                                // Start inline assembly
        {
                mov al, c                        // Move the character into the al register
                xor al, 00100000b; // XOR the al register with 00100000 which will invert bit 5
        }                                        // End inline assembly
}
else
        return c;                        // If the character was already uppercase, return the same character
}

That function works as expected but is limited to x86 based machines.
Is there a way to do the same thing with C?

Thanks in advance for any advice you can give.

Cheeseh 02-04-2004 01:12

Re: Binary numbers
 
c ^ 00100000b

(^ should be bitwise XOR I believe?)

/edit: *cough* crap how do you specify binary numbers in C lol o_O

whatever: c ^ 32 should do the same thing

Lazy 02-04-2004 01:18

Re: Binary numbers
 
Yeah that did it, thanks.
I know how to convert from binary to decimal but I was hoping that I wouldn't have to.

Pierre-Marie Baty 02-04-2004 04:03

Re: Binary numbers
 
Meh!
Quote:

Originally Posted by Lazy
In the following example the function takes a lowercase character and XORs it with 00100000 to convert it to an uppercase character.
(...)
Is there a way to do the same thing with C?

Sure there is :D
Code:

#include <string.h>
 
  char lowerCaseCharacter = 'c';
 
  char upperCaseCharacter = toupper (lowerCaseCharacter);

Works on any architecture, any character set and even unicode. Fast.
Why bother with non-portable code ? :)

Lazy 02-04-2004 04:07

Re: Binary numbers
 
I already know about the standard C library functions and have used that one many times.

I used my function to describe the question better,
even still I cannot figure out how to use binary numbers without having to convert them to decimal first.

I still think my function uses less instructions though :)

botmeister 02-04-2004 08:30

Re: Binary numbers
 
Quote:

I cannot figure out how to use binary numbers without having to convert them to decimal first
All numbers are stored as binary values inside your computer, therefore a "conversion to decimal" is only a way of thinking about numbers, and not a real conversion to decimal.

I suggest that you get used to working with the hexadecimal representation instead of decimal or binary.

Why use Hex?
http://www.cs.ualberta.ca/~casey/c101/101notes/hex.html

Decimal to Hex table
http://www.jaworski.com/htmlbook/dec-hex.htm

C/C++ has all the operators you need to do bit level operations without resorting to inline asm code.

Arithmetic + Binary plus (add)
- Binary minus (subtract)
* Multiply
/ Divide
% Remainder (modulus)
Bitwise << Shift left
>> Shift right
& Bitwise AND
^ Bitwise XOR (exclusive OR)
| Bitwise inclusive OR
Logical && Logical AND
|| Logical OR
Assignment = Assignment
*= Assign product
/= Assign quotient
%= Assign remainder (modulus)
+= Assign sum
-= Assign difference
<<= Assign left shift
>>= Assign right shift
&= Assign bitwise AND
^= Assign bitwise XOR
|= Assign bitwise OR
Relational < Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
== Equal to
!= Not equal to

Lazy 02-04-2004 08:42

Re: Binary numbers
 
Thanks for the info, though I know how to use all of the operators I just wondered why I cannot just use a binary number.

I knew that XORing the character with 00100000 would reverse the case. But wouldn't it be easier to just use that binary number without having to convert it first?

I have learned both numbering systems already but as I am currently learning x86 assembly binary comes up quite often and if I don't use it every now and again I'll forget it.

Cheeseh 02-04-2004 13:31

Re: Binary numbers
 
<background reading>

some C compilers allow this some don't, I don't know about MSVC 6.0... but some compilers might allow binary numbers to be declared like this (e.g. those similar GCC)

0b<binary number>

MS c++/c# .Net might allow binarys like this

<binary number>B

that doesn't work on MSVC 6... :(

(we are looking for pre/postfixes on numbers e.g. 0x for hex, 0 for octal 0.0f for float etc, what's the one for binary???)

koraX 02-04-2004 21:49

Re: Binary numbers
 
I just want to add that if you want to display number in binary format, you can't do it with printf.
But you can do it with itoa
http://www.cplusplus.com/ref/cstdlib/itoa.html

also AFAIK you can't specify binary number (like 00101b) I think only 1234, 0x0033 and 07654 works (deciman, hex and octal)
IMO it's not in ANSI C/C++
http://www.cs.colorado.edu/~eliuser/c_html/c.html
http://www.comnets.rwth-aachen.de/do.../contents.html

Austin 03-04-2004 12:09

Re: Binary numbers
 
Quote:

Originally Posted by Lazy
Thanks for the info, though I know how to use all of the operators I just wondered why I cannot just use a binary number.

What Lazy is asking, is how do I represent BINARY CONSTANTS in the c/c++ language.

lazy, you can't!!!
koraX is korrect!
c/c++ doesn't have a way to do this!

It has been proposed by the standards committee but as far as I know it hasn't been implemented by any compiler.

You have to convert the binary number to octal or hex, first then use the familiar c/c++ CONSTANTS SYNTAX:
0x<hex number>
0<octal number>


All times are GMT +2. The time now is 07:47.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.