.:: Bots United ::.  
filebase forums discord server github wiki web
cubebot epodbot fritzbot gravebot grogbot hpbbot ivpbot jkbotti joebot
meanmod podbotmm racc rcbot realbot sandbot shrikebot soulfathermaps yapb

Go Back   .:: Bots United ::. > Developer's Farm > General Programming
General Programming Help others and get yourself helped here!

Reply
 
Thread Tools
Binary numbers
Old
  (#1)
Lazy
Member
 
Lazy's Avatar
 
Status: Offline
Posts: 236
Join Date: Jan 2004
Location: Toronto, Ontario, Canada
Default Binary numbers - 02-04-2004

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.

Last edited by Lazy; 02-04-2004 at 02:06.. Reason: Bad title
  
Reply With Quote
Re: Binary numbers
Old
  (#2)
Cheeseh
[rcbot]
 
Cheeseh's Avatar
 
Status: Offline
Posts: 361
Join Date: Dec 2003
Location: China
Default Re: Binary numbers - 02-04-2004

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

Last edited by Cheeseh; 02-04-2004 at 02:15.. Reason: Stupidity
  
Reply With Quote
Re: Binary numbers
Old
  (#3)
Lazy
Member
 
Lazy's Avatar
 
Status: Offline
Posts: 236
Join Date: Jan 2004
Location: Toronto, Ontario, Canada
Default Re: Binary numbers - 02-04-2004

Yeah that did it, thanks.
I know how to convert from binary to decimal but I was hoping that I wouldn't have to.
  
Reply With Quote
Re: Binary numbers
Old
  (#4)
Pierre-Marie Baty
Roi de France
 
Pierre-Marie Baty's Avatar
 
Status: Offline
Posts: 5,049
Join Date: Nov 2003
Location: 46°43'60N 0°43'0W 0.187A
Default Re: Binary numbers - 02-04-2004

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
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 ?



RACC home - Bots-United: beer, babies & bots (especially the latter)
"Learn to think by yourself, else others will do it for you."
  
Reply With Quote
Re: Binary numbers
Old
  (#5)
Lazy
Member
 
Lazy's Avatar
 
Status: Offline
Posts: 236
Join Date: Jan 2004
Location: Toronto, Ontario, Canada
Default Re: Binary numbers - 02-04-2004

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
  
Reply With Quote
Re: Binary numbers
Old
  (#6)
botmeister
Ex-Council Member
 
botmeister's Avatar
 
Status: Offline
Posts: 1,090
Join Date: Nov 2003
Location: Canada
Default Re: Binary numbers - 02-04-2004

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


Maker of the (mEAn) Bot.Admin Manager

"In theory, there is no difference between theory and practice. But, in practice, there is." - Jan L.A. van de Snepscheut

Last edited by botmeister; 02-04-2004 at 09:31..
  
Reply With Quote
Re: Binary numbers
Old
  (#7)
Lazy
Member
 
Lazy's Avatar
 
Status: Offline
Posts: 236
Join Date: Jan 2004
Location: Toronto, Ontario, Canada
Default Re: Binary numbers - 02-04-2004

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.
  
Reply With Quote
Re: Binary numbers
Old
  (#8)
Cheeseh
[rcbot]
 
Cheeseh's Avatar
 
Status: Offline
Posts: 361
Join Date: Dec 2003
Location: China
Default Re: Binary numbers - 02-04-2004

<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???)

Last edited by Cheeseh; 02-04-2004 at 14:45..
  
Reply With Quote
Re: Binary numbers
Old
  (#9)
koraX
Member
 
koraX's Avatar
 
Status: Offline
Posts: 145
Join Date: Jan 2004
Location: Slovak Republic
Default Re: Binary numbers - 02-04-2004

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


kXBot
koraX's utils
- see my homepage for other projects (OpenGL CSG Editor, FAT16 Sim, NNetwork Sim, ...)

Last edited by koraX; 02-04-2004 at 22:51..
  
Reply With Quote
Re: Binary numbers
Old
  (#10)
Austin
Moderator
 
Austin's Avatar
 
Status: Offline
Posts: 403
Join Date: Nov 2003
Default Re: Binary numbers - 03-04-2004

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>

Last edited by Austin; 03-04-2004 at 13:14..
  
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
vBulletin Skin developed by: vBStyles.com